面向过程
<?php
$server = new swoole_websocket_server("0.0.0.0", 9501);
$server->on('open','onOpen');
function onOpen($server, $request)
{
echo "server: handshake success with fd{$request->fd}\n";
}
$server->on('message','onMessage');
function onMessage($server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
}
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
面向对象封装后
<?php
/**
* websocket 封装
*/
class Websocket
{
CONST HOST = '0.0.0.0';
CONST PORT = 9501;
public $ws = null;
public function __construct()
{
$this->ws = new swoole_websocket_server('0.0.0.0', 9501);
if ($this->ws) {
$this->ws->set([
'worker_num'=>2,
'task_worker_num'=>2
]);
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("task", [$this, 'onTask']);
$this->ws->on("finish", [$this, 'onFinish']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
}
/**
* 监听ws连接事件
* @param [type] $ws [description]
* @param [type] $request [description]
* @return [type] [description]
*/
public function onOpen($ws,$request)
{
swoole_timer_tick(5000,function($timeid){
echo "timerId:{$timeid}";
});
$countent = [
'date'=>date('Y-m-d H:i:s'),
'get'=>$request->get,
'post'=>$request->post,
'header'=>$request->header
];
swoole_async_writefile(__DIR__.'/access.log',json_encode($countent),function(){},FILE_APPEND);
var_dump($request->fd);
}
/**
* 监听客户端消息事件
* @param [type] $ws [description]
* @param [type] $frame [description]
* @return [type] [description]
*/
public function onMessage($ws,$frame)
{
echo "clien_push_message:{$frame->data}\n";
$data = [
'task'=>1,
'fd'=>$frame->fd
];
$ws->task($data);
swoole_timer_after(500,function() use ($ws,$frame)
{
$ws->push($frame->fd,"5s after:{$frame->fd}");
});
$ws->push($frame->fd,"server_push_message:".date('Y-m-d H:i:s'));
}
/**
* task
* @param [type] $serv [对象]
* @param [type] $taskId [taskid]
* @param [type] $workerId [workderid]
* @param [type] $data [数据]
* @return [type] [description]
*/
public function onTask($serv,$taskId,$workerId,$data)
{
print_r($data);
sleep(15);
return "on task finish";
}
public function onFinish($serv,$taskId,$data)
{
echo "taskId:{$taskId}\n";
echo "finish-data-success:{$data}\n";
}
/**
* 监听关闭事件
* @param [type] $ws [description]
* @param [type] $frame [description]
* @return [type] [description]
*/
public function onClose($ws,$fd)
{
echo "close clientId:{$fd}\n";
}
}
$obj = new Websocket();
swoole定时器 ,每5秒在shell终端打印定时器id
swoole_timer_tick(5000,function($timeid){
echo "timerId:{$timeid}";
});
swoole文件异步 已追加形式 写入到当前目录下的access.log文件中,
$countent = [
'date'=>date('Y-m-d H:i:s'),
'get'=>$request->get,
'post'=>$request->post,
'header'=>$request->header
];
swoole_async_writefile(__DIR__.'/access.log',json_encode($countent),function({},FILE_APPEND);
swoole异步定制器 ,只执行一次 ; use ($ws,$frame) 闭包函数引入
swoole_timer_after(500,function() use ($ws,$frame)
{
$ws->push($frame->fd,"5s after:{$frame->fd}");
});
swoole task异步处理事件
$ws->task($data); //看作是发布异步任务
//自动监听task任务,并进行处理
public function onTask($serv,$taskId,$workerId,$data)
{
print_r($data);
sleep(15);
return "on task finish";
}