首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
redis抛出异常。如何优化下面代码。
### 问题描述 ![](/storage/article-column/zpvOOa80SSZTFP1B0KsF719s8Kd7rsZwjEv2PNZ2.png) ### Swoole版本,PHP版本,以及操作系统版本信息 php:7.1 swoole:4.5.1 ### 相关代码 ```php <?php ini_set("max_execution_time", 0); ini_set("date.timezone", "Asia/Shanghai"); 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); $channel = new Swoole\Coroutine\Channel(10); $server->handle( '/ws', function ($request, $ws) use ($channel) { $ws->upgrade(); global $wsObjects; $wsObjects[getObjectId($ws)] = $ws; while (true) { try { $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 ($channel, $frame, $arr) { $channel->push(['k' => 'mid:'.$arr['matchid'].':ios', 'v' => $arr['UDID']]); }); } elseif (isset($arr['android_id']) && isset($arr['matchid'])) { go(function () use ($channel, $frame, $arr) { $channel->push( ['k' => 'mid:'.$arr['matchid'].':android', 'v' => $arr['android_id']] ); }); } go(function () use ($channel) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('123456'); $data = $channel->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); } } } catch (\Swoole\Coroutine\Socket\Exception $e) { file_put_contents('./log', $e->getTraceAsString(), FILE_APPEND); $ws->close; break; } } } ); $server->start(); }); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 帮忙指出代码中的不足。如何健壮代码
发布于4年前 · 4 次浏览 · 来自
提问
highkick
### 问题描述 ![](/storage/article-column/zpvOOa80SSZTFP1B0KsF719s8Kd7rsZwjEv2PNZ2.png) ### Swoole版本,PHP版本,以及操作系统版本信息 php:7.1 swoole:4.5.1 ### 相关代码 ```php <?php ini_set("max_execution_time", 0); ini_set("date.timezone", "Asia/Shanghai"); 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); $channel = new Swoole\Coroutine\Channel(10); $server->handle( '/ws', function ($request, $ws) use ($channel) { $ws->upgrade(); global $wsObjects; $wsObjects[getObjectId($ws)] = $ws; while (true) { try { $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 ($channel, $frame, $arr) { $channel->push(['k' => 'mid:'.$arr['matchid'].':ios', 'v' => $arr['UDID']]); }); } elseif (isset($arr['android_id']) && isset($arr['matchid'])) { go(function () use ($channel, $frame, $arr) { $channel->push( ['k' => 'mid:'.$arr['matchid'].':android', 'v' => $arr['android_id']] ); }); } go(function () use ($channel) { $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('123456'); $data = $channel->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); } } } catch (\Swoole\Coroutine\Socket\Exception $e) { file_put_contents('./log', $e->getTraceAsString(), FILE_APPEND); $ws->close; break; } } } ); $server->start(); }); ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 帮忙指出代码中的不足。如何健壮代码
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2020-10-23
鲁飞
使用redis连接池。
赞
0
回复
微信公众号
热门内容
作者其它话题
- 进程(9501端口)一段时间后退出;
- $ws为何获取不到数据
- 进程(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一直是同一个。没用使用到多进程啊。