首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
$ws为何获取不到数据
### 问题描述 **[20-Oct-2020 08:50:21 UTC] PHP Warning: Swoole\Http\Response::recv(): fd[975] is not a websocket conncetion** 请问一下下面代码 $ws为何获取不到数据。另外请问一下下面代码是否正确。小白实在感谢 ### Swoole版本,PHP版本,以及操作系统版本信息 PHP:7.4 Swoole:4.5.1 ### 相关代码 ```php <?php function getObjectId(\Swoole\Http\Response $response) { if (PHP_VERSION_ID < 70200) { $id = spl_object_hash($response); } else { $id = spl_object_id($response); } return $id; } Co::set([ 'hook_flags'=> SWOOLE_HOOK_ALL, 'socket_timeout' => 8, 'daemonize' => 1, 'max_coroutine' => 500000 ]); Co\run(function (){ $server = new Co\Http\Server("0.0.0.0", 9501, false); $chan = new Swoole\Coroutine\Channel(10); $server->handle('/', function ($request, $ws) use ($chan) { $ws->upgrade(); global $wsObjects; $wsObjects[getObjectId($ws)] = $ws; $i = 0; while (true) { $frame = $ws->recv(); if (!isset($frame->opcode)) { $ws->close(); break; } elseif ($frame->opcode === 9) { $pFrame = new Swoole\WebSocket\Frame; $pFrame->opcode = 10; $ws->push($pFrame); } elseif ($frame->opcode === 8) { $ws->close(); break; } elseif ($frame === '' or $frame->data === '') { $ws->close(); break; } elseif ($frame === false) { echo "error : " . swoole_last_error() . "\n"; break; } else { if ($frame->data == 'close') { $ws->close(); return; } parse_str($frame->data, $arr); if (isset($arr['UDID']) && isset($arr['matchid'])) { go(function() use ($chan, $frame, $arr) { $chan->push(['k' => 'mid:'.$arr['matchid'].':ios', 'v' => $arr['UDID']]); }); } elseif (isset($arr['android_id']) && isset($arr['matchid'])) { go(function() use ($chan, $frame, $arr) { $chan->push(['k' => 'mid:'.$arr['matchid'].':android', 'v' => $arr['android_id']]); }); } go(function() use ($chan) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $data = $chan->pop(); if (isset($data['k'])) { $redis->sadd($data['k'], $data['v']); } }); for($i=0;$i<2;$i++) { $ws->push("fd: ".$frame->fd. "this is test message:".rand(1000, 9999), 2); } } } }); $server->start(); }); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 正常连接数十万的socket连接
发布于4年前 · 0 次浏览 · 来自
提问
highkick
### 问题描述 **[20-Oct-2020 08:50:21 UTC] PHP Warning: Swoole\Http\Response::recv(): fd[975] is not a websocket conncetion** 请问一下下面代码 $ws为何获取不到数据。另外请问一下下面代码是否正确。小白实在感谢 ### Swoole版本,PHP版本,以及操作系统版本信息 PHP:7.4 Swoole:4.5.1 ### 相关代码 ```php <?php function getObjectId(\Swoole\Http\Response $response) { if (PHP_VERSION_ID < 70200) { $id = spl_object_hash($response); } else { $id = spl_object_id($response); } return $id; } Co::set([ 'hook_flags'=> SWOOLE_HOOK_ALL, 'socket_timeout' => 8, 'daemonize' => 1, 'max_coroutine' => 500000 ]); Co\run(function (){ $server = new Co\Http\Server("0.0.0.0", 9501, false); $chan = new Swoole\Coroutine\Channel(10); $server->handle('/', function ($request, $ws) use ($chan) { $ws->upgrade(); global $wsObjects; $wsObjects[getObjectId($ws)] = $ws; $i = 0; while (true) { $frame = $ws->recv(); if (!isset($frame->opcode)) { $ws->close(); break; } elseif ($frame->opcode === 9) { $pFrame = new Swoole\WebSocket\Frame; $pFrame->opcode = 10; $ws->push($pFrame); } elseif ($frame->opcode === 8) { $ws->close(); break; } elseif ($frame === '' or $frame->data === '') { $ws->close(); break; } elseif ($frame === false) { echo "error : " . swoole_last_error() . "\n"; break; } else { if ($frame->data == 'close') { $ws->close(); return; } parse_str($frame->data, $arr); if (isset($arr['UDID']) && isset($arr['matchid'])) { go(function() use ($chan, $frame, $arr) { $chan->push(['k' => 'mid:'.$arr['matchid'].':ios', 'v' => $arr['UDID']]); }); } elseif (isset($arr['android_id']) && isset($arr['matchid'])) { go(function() use ($chan, $frame, $arr) { $chan->push(['k' => 'mid:'.$arr['matchid'].':android', 'v' => $arr['android_id']]); }); } go(function() use ($chan) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $data = $chan->pop(); if (isset($data['k'])) { $redis->sadd($data['k'], $data['v']); } }); for($i=0;$i<2;$i++) { $ws->push("fd: ".$frame->fd. "this is test message:".rand(1000, 9999), 2); } } } }); $server->start(); }); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 正常连接数十万的socket连接
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2020-10-21
鲁飞
找个websocket客户端进行连接。不是直接http请求。
赞
0
回复
微信公众号
热门内容
作者其它话题
- 进程(9501端口)一段时间后退出;
- redis抛出异常。如何优化下面代码。
- 进程(9501端口)一段时间后会自己退出;
- 协程方式压测websocket报错
暂无回复的问答
- CodeGalaxy K3s 轻量集群节点之间如何实现负载均衡
- 关于openssl CURL WARNING swSSL_connect: SSL_connect(fd=69) failed. Error: error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small[1|394]
- 多个模型如何进行事务异常回退?
- websocket开启wss报错
- 协程tcp服务器如何使用多进程?recv()方法接收信息,打印出来的pid一直是同一个。没用使用到多进程啊。