ThinkPHP6初步运行swoole运行初步Websocket
初步启动swoole
tp6官网composer安装swoole
2.使用php think swoole 启动swoole 像 start stop restart 命令均失效
3.启动后可在浏览器内根据IP+端口访问代码文件进行测试
启动Websocket
复制websocket后台测试基本代码
/创建WebSocket Server对象,监听0.0.0.0:9502端口$ws = new Swoole\WebSocket\Server('0.0.0.0', 9502, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL);//前端是wss需要配置证书路径$ws->set(array( 'ssl_cert_file' => '/www/server/panel/vhost/ssl/tp.am.zjm0305.top/fullchain.pem', 'ssl_key_file' => '/www/server/panel/vhost/ssl/tp.am.zjm0305.top/privkey.pem',));//监听WebSocket连接打开事件$ws->on('Open', function ($ws, $request) { $ws->push($request->fd, "hello, welcome\n");});//监听WebSocket消息事件$ws->on('Message', function ($ws, $frame) { echo "Message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}");});//监听WebSocket连接关闭事件$ws->on('Close', function ($ws, $fd) { echo "client-{$fd} is closed\n";});$ws->start();
前台测试基本代码
Title
注意开放对应端口即可
来源地址:https://blog.csdn.net/weixin_43876402/article/details/126390312