🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 回调函数 WebSocketServer 有三个回调函数: - Open:连接握手成功后执行 - Message:接收到消息时执行 - Close:连接关闭时执行,非 WebSocket 连接不会执行 下面是 [DEMO](https://github.com/mix-php/mix/blob/v1/apps/websocketd/commands/ServiceCommand.php) 中创建服务的部分代码 ,这里绑定了三个回调函数。 ~~~ // 创建服务 $server = app()->createObject('webSocketServer'); $server->on('Open', [$this, 'onOpen']); $server->on('Message', [$this, 'onMessage']); $server->on('Close', [$this, 'onClose']); // 启动服务 $server->start(); ~~~ >[success] 源码 DEMO 中关于回调函数中的代码是在生产环境中验证过的最佳范例,全部代码用户都可根据自己的业务需求去修改。