## **一,WebSocket服务器创建** ``` <?php // +---------------------------------------------------------------------- // | najing [ 通用后台管理系统 ] // +---------------------------------------------------------------------- // | Copyright (c) 2020 http://www.najingquan.com All rights reserved. // +---------------------------------------------------------------------- // | Author: 救火队队长 // +---------------------------------------------------------------------- namespace server; /** * 概要描述:WebSocket服务器 * @author: 救火队队长 * @since: 2020-05-23 22:45 */ class WebSocketServer { protected $serv = null; //Swoole\Server对象 protected $host = '0.0.0.0'; //监听对应外网的IP 0.0.0.0监听所有ip protected $port = 9604; //监听端口号 public function __construct() { //创建websocket服务器对象,监听0.0.0.0:9604端口 $this->serv = new \Swoole\Websocket\Server($this->host, $this->port); //设置参数 //如果业务代码是全异步 IO 的,worker_num设置为 CPU 核数的 1-4 倍最合理 //如果业务代码为同步 IO,worker_num需要根据请求响应时间和系统负载来调整,例如:100-500 //假设每个进程占用 40M 内存,100 个进程就需要占用 4G 内存 $this->serv->set(array( 'worker_num' => 4, //设置启动的worker进程数。【默认值:CPU 核数】 'max_request' => 10000, //设置每个worker进程的最大任务数。【默认值:0 即不会退出进程】 'daemonize' => 0, //开启守护进程化【默认值:0】 )); //监听WebSocket连接打开事件 $this->serv->on('open', function ($serv, $req) { echo "connection open: {$req->fd}" . PHP_EOL; }); //监听WebSocket消息事件 //客户端向服务器端发送信息时,服务器端触发 onMessage 事件回调 //服务器端可以调用 $server->push() 向某个客户端(使用 $fd 标识符)发送消息,长度最大不得超过 2M $this->serv->on('message', function ($serv, $frame) { echo "Message: {$frame->data}". PHP_EOL; $this->serv->push($frame->fd, "server: {$frame->data}"); }); //监听WebSocket连接关闭事件 $this->serv->on('close', function ($serv, $fd) { echo "connection close: {$fd}". PHP_EOL; }); //启动服务 $this->serv->start(); } } $webSocketServer = new WebSocketServer(); ``` ## **二,运行WebSocket服务** >[danger] **需要用php CLI模式运行** ![](https://img.kancloud.cn/42/93/4293c2b905840112709ca35acc7c002c_521x34.png) ## **三,测试** 可以使用 Chrome 浏览器进行测试,JS 代码为: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <script> var wsServer = 'ws://212.64.29.192:9604'; var websocket = new WebSocket(wsServer); websocket.onopen = function (evt) { console.log("Connected to WebSocket server."); //发送消息 websocket.send("我发送的消息"); }; websocket.onclose = function (evt) { console.log("Disconnected"); }; websocket.onmessage = function (evt) { console.log('Retrieved data from server: ' + evt.data); }; websocket.onerror = function (evt, e) { console.log('Error occured: ' + evt.data); }; </script> </body> <html> ``` 打开浏览器的控制台,可以看到客户端已经正常收发消息 ![](https://img.kancloud.cn/40/92/4092460f9c6cf9a100818567a6d77961_1139x388.png)