🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 服务启用 * 声明(注:访问地址要外网可访问的,接口可自行设置) ~~~ $socket = new swoole_websocket_server('127.0.0.1',9502); ~~~ * 配置参数 ~~~ $obj = []; $obj['max_conn'] = 1024; //>最大连接数(不可超过ulimit -u的值,实际连接数只有一半) $obj['worker_num'] = 8; //>worker进程数(一般为CPU核数的1-4倍) $obj['max_request'] = 10000; //>每个worker执行请求数后结束运行 $obj['daemonize'] = 0; //>守护进程化(1为执行php start.php后直接转入后台进程运行) $obj['dispatch_mode'] = 2; //>进程数据包分配模式(1平均分配2按FD取摸固定分配,3抢占式分配) $socket->set($obj); ~~~ * 连接处理 ~~~ $socket->on('open', function (swoole_websocket_server $socket, $request) use($redis) { $fd = $request->fd; //>客户端id include ROOT_PATH.'/include/socket/open.php'; }); ~~~ * 通信处理 ~~~ $socket->on('message', function (swoole_websocket_server $socket, $frame) use($redis) { $fd = $frame->fd; //>客户端id $data = $frame->data; //>接收信息 // $opcode = $frame->opcode; // $finish = $frame->finish; include ROOT_PATH.'/include/socket/message.php'; }); ~~~ * 关闭处理 ~~~ $socket->on('close', function (swoole_websocket_server $socket, $fd) use($redis) { include ROOT_PATH.'/include/socket/close.php'; }); ~~~ * 启用通信 ~~~ return $socket->start(); ~~~