多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
``` php <?php $server = new swoole_websocket_server("0.0.0.0", 9501); $server->set(array( 'daemonize' => false, 'worker_num' => 2, )); $server->on('Start', function (swoole_websocket_server $server) { echo "Server Start... \n"; swoole_set_process_name("swoole_websocket_server"); }); $server->on('ManagerStart', function (swoole_websocket_server $server) { echo "ManagerStart\n"; }); $server->on('WorkerStart', function (swoole_websocket_server $server, $worker_id) { echo "WorkerStart \n"; if ($server->worker_id == 0){ swoole_timer_tick(10000,function($id) use ($server) { echo "test timer\n"; }); } }); $server->on('Open', function (swoole_websocket_server $server, $request) { echo "server: handshake success with fd{$request->fd}\n"; }); $server->on('Message', function (swoole_websocket_server $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(); ``` shell里直接运行`php swoole_ws_server.php`启动即可。如果设置了后台运行,可以使用下列命令强杀进程: ``` kill -9 $(ps aux|grep swoole|grep -v grep|awk '{print $2}') ``` 或者重新启动worker进程: ``` kill -10 $(ps aux|grep swoole_websocket_server|grep -v grep|awk '{print $2}') ``` 输出: ``` [2017-06-01 22:06:21 $2479.0] NOTICE Server is reloading now. WorkerStart WorkerStart ``` 注意: - onMessage回调函数为必选,当服务器收到来自客户端的数据帧时会回调此函数。 ``` /** * @param $server * @param $frame 包含了客户端发来的数据帧信息;使用$frame->fd获取fd;$frame->data获取数据内容 */ function onMessage(swoole_server $server, swoole_websocket_frame $frame) ``` - 使用`$server->push()`向客户端发送消息。长度最大不得超过2M。发送成功返回true,发送失败返回false。 ``` function swoole_websocket_server->push(int $fd, string $data, int $opcode = 1, bool $finish = true); ```