首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
问一个 websocket 的问题
server mode 在 SWOOLE_PROCESS 模式下 在自定义 Process 进程中 调用 Swoole/Server->push() 发送消息 会不会 存在一部分fd 收不到消息的问题呢
发布于6年前 · 1 次浏览 · 来自
提问
nicoi
server mode 在 SWOOLE_PROCESS 模式下 在自定义 Process 进程中 调用 Swoole/Server->push() 发送消息 会不会 存在一部分fd 收不到消息的问题呢
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-04-20
鲸落
Server 端: <?php //创建websocket服务器对象,监听0.0.0.0:9502端口 $ws = new Swoole\WebSocket\Server("0.0.0.0", 9502); $process = new \Swoole\Process(function (\Swoole\Process $process) use ($ws) { $socket = $process->exportSocket(); while (true) { $msg = $socket->recv(); foreach ($ws->connections as $connection) { $ws->push($connection, $msg); } } }, false, 2, 1); $ws->addProcess($process); //监听WebSocket连接打开事件 $ws->on('open', function ($ws, $request) { var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "hello, welcome\n"); }); //监听WebSocket消息事件 $ws->on('message', function ($ws, $frame) use($process) { $socket = $process->exportSocket(); $socket -> send($frame->data); 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(); client端: <?php go(function () { $client = new Swoole\Coroutine\Http\Client("127.0.0.1", 9502); $ret = $client->upgrade("/"); var_dump($ret); if ($ret) { while(true) { var_dump($client->push("hello")); var_dump($client->recv()); co::sleep(1); } } }); 本地测试了是没有啥问题的,你可以试一下
赞
1
回复
2020-04-22
nicoi
回复
鲸落
非常感谢
赞
0
回复