首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
协程websocket并发读写异常的问题,文档有这个说明,但怎么解决?
### 问题描述 Uncaught Swoole\Error: Socket#6 has already been bound to another coroutine#4, writing of the same socket in coroutine#54 at the same time is not all ### Swoole版本,PHP版本,以及操作系统版本信息 4.5 7.3 ### 相关代码 ```php 请将代码粘贴至此处(请勿用截图) ``` ### 你期待的结果是什么?实际看到的错误信息又是什么?
发布于6年前 · 145 次浏览 · 来自
提问
Y js
### 问题描述 Uncaught Swoole\Error: Socket#6 has already been bound to another coroutine#4, writing of the same socket in coroutine#54 at the same time is not all ### Swoole版本,PHP版本,以及操作系统版本信息 4.5 7.3 ### 相关代码 ```php 请将代码粘贴至此处(请勿用截图) ``` ### 你期待的结果是什么?实际看到的错误信息又是什么?
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-07-01
郭新华
两个协程不能同时对一个socket读或者写,解决办法是这个socket单独创建2个协程,一个负责读(co read) 一个负责写(co write),同时`co read`,和`co write`都创建一个channel,并且都while true pop/push自己的channel, 伪代码: ```php //读协程 go(function(){ while(true){ $data = $socket->read(); $channel_read->push($data); } }); //写协程 go(function(){ while(true){ $data = $channel_write->pop(); $socket->wirte($data); } }); ``` 然后其他的地方需要读、写这socket的时候 需要通过这两个 channel和`co read/write`通讯。
赞
1
回复