首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
WebSocket ws://*******:9501 connection failed: Invalid frame header
错误日志:ERROR zm_deactivate_swoole (ERROR 9003): worker process is terminated by exit()/die(). 出现这种错误我现在都是直接kill 掉占9501 端口的进程,重启服务器脚本,暂时可以推送数据了,过几个小时,又出现了上面的错误, 服务器代码 include_once PROJECT_PATH . '/lib/box/redis.php'; //链接redis $redis=new MY_redisCluster(); $flag=$redis->connect(array('host'=>'127.0.0.1','port'=>6378)); $server = new swoole_websocket_server("0.0.0.0", 9501); $server->set(array( 'worker_num' =>8, 'dispatch_mode' =>2, 'daemonize' =>0, 'log_file' =>'/www/webV1/swoole/swoole.log' )); $server->on('open', function (swoole_websocket_server $server, $request) { echo "server: handshake success with fd{$request->fd}\n"; }); $server->on('message', function (swoole_websocket_server $server, $frame) { global $server; global $redis; $reqfd = $frame->fd; echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $fds=$redis->get("fds"); //redis存储客户端socketid if(empty($fds)){ $allfds[$reqfd]=$reqfd; $allfds=json_encode($allfds); $redis->set("fds",$allfds); }else{ $fds=json_decode($fds,true); if(!isset($fds[$reqfd])){ $fds[$reqfd]=$reqfd; $fds=json_encode($fds); $redis->set("fds",$fds); } } $fds=$redis->get("fds"); if(!empty($frame)){ foreach($server->connections as $fd) { //广播 $server->push($fd, $frame->data); } } }); $server->on('Request', function(swoole_http_request $request,swoole_http_response $response) { global $server; global $redis; //获取推送的数据 $request=(array)($request); //获取get信息推送 if(isset($request['get']) && $request['get']['type']=="pushgoods" && !empty($request['get']['numiid'])){ $data=array('action'=>"pushgoods",'numiid'=>$request['get']['numiid']); $data=json_encode($data); //redis读取所有客户端socketid $fds=$redis->get("fds"); $fds=json_decode($fds,true); //广播 if(!empty($fds)){ foreach($fds as $connection){ $flag=$server->push($connection, $data); } } $response->end($data); } }); // //服务端请求更新数据,这里是http方式 $server->on('close', function ($ser, $fd) { echo "client {$fd} closed\n"; }); $server->start(); //客户端代码 var url='ws://*******:9501'; ws = new WebSocket(url); initEventHandle(); function initEventHandle() { ws.onclose = function (evt) { reconnect(wsUri); }; ws.onerror = function (evt) { reconnect(wsUri); }; ws.onopen = function (evt) { //心跳检测重置 heartCheck.reset().start(); }; ws.onmessage = function (evt) { //如果获取到消息,心跳检测重置 //拿到任何消息都说明当前连接是正常的 heartCheck.reset().start(); onMessage(evt); } } function reconnect(url) { if(lockReconnect) return; lockReconnect = true; //没连接上会一直重连,设置延迟避免请求过多 setTimeout(function () { createWebSocket(url); lockReconnect = false; }, 2000); } //心跳检测 var heartCheck = { timeout: 60000,//60秒 timeoutObj: null, serverTimeoutObj: null, reset: function(){ clearTimeout(this.timeoutObj); clearTimeout(this.serverTimeoutObj); return this; }, start: function(){ var self = this; this.timeoutObj = setTimeout(function(){ //这里发送一个心跳,后端收到后,返回一个心跳消息, //onmessage拿到返回的心跳就说明连接正常 ws.send("HeartBeat"); self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了 ws.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次 }, self.timeout) }, this.timeout) } }
发布于6年前 · 1 次浏览 · 来自
提问
星
星空之钥丶
错误日志:ERROR zm_deactivate_swoole (ERROR 9003): worker process is terminated by exit()/die(). 出现这种错误我现在都是直接kill 掉占9501 端口的进程,重启服务器脚本,暂时可以推送数据了,过几个小时,又出现了上面的错误, 服务器代码 include_once PROJECT_PATH . '/lib/box/redis.php'; //链接redis $redis=new MY_redisCluster(); $flag=$redis->connect(array('host'=>'127.0.0.1','port'=>6378)); $server = new swoole_websocket_server("0.0.0.0", 9501); $server->set(array( 'worker_num' =>8, 'dispatch_mode' =>2, 'daemonize' =>0, 'log_file' =>'/www/webV1/swoole/swoole.log' )); $server->on('open', function (swoole_websocket_server $server, $request) { echo "server: handshake success with fd{$request->fd}\n"; }); $server->on('message', function (swoole_websocket_server $server, $frame) { global $server; global $redis; $reqfd = $frame->fd; echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n"; $fds=$redis->get("fds"); //redis存储客户端socketid if(empty($fds)){ $allfds[$reqfd]=$reqfd; $allfds=json_encode($allfds); $redis->set("fds",$allfds); }else{ $fds=json_decode($fds,true); if(!isset($fds[$reqfd])){ $fds[$reqfd]=$reqfd; $fds=json_encode($fds); $redis->set("fds",$fds); } } $fds=$redis->get("fds"); if(!empty($frame)){ foreach($server->connections as $fd) { //广播 $server->push($fd, $frame->data); } } }); $server->on('Request', function(swoole_http_request $request,swoole_http_response $response) { global $server; global $redis; //获取推送的数据 $request=(array)($request); //获取get信息推送 if(isset($request['get']) && $request['get']['type']=="pushgoods" && !empty($request['get']['numiid'])){ $data=array('action'=>"pushgoods",'numiid'=>$request['get']['numiid']); $data=json_encode($data); //redis读取所有客户端socketid $fds=$redis->get("fds"); $fds=json_decode($fds,true); //广播 if(!empty($fds)){ foreach($fds as $connection){ $flag=$server->push($connection, $data); } } $response->end($data); } }); // //服务端请求更新数据,这里是http方式 $server->on('close', function ($ser, $fd) { echo "client {$fd} closed\n"; }); $server->start(); //客户端代码 var url='ws://*******:9501'; ws = new WebSocket(url); initEventHandle(); function initEventHandle() { ws.onclose = function (evt) { reconnect(wsUri); }; ws.onerror = function (evt) { reconnect(wsUri); }; ws.onopen = function (evt) { //心跳检测重置 heartCheck.reset().start(); }; ws.onmessage = function (evt) { //如果获取到消息,心跳检测重置 //拿到任何消息都说明当前连接是正常的 heartCheck.reset().start(); onMessage(evt); } } function reconnect(url) { if(lockReconnect) return; lockReconnect = true; //没连接上会一直重连,设置延迟避免请求过多 setTimeout(function () { createWebSocket(url); lockReconnect = false; }, 2000); } //心跳检测 var heartCheck = { timeout: 60000,//60秒 timeoutObj: null, serverTimeoutObj: null, reset: function(){ clearTimeout(this.timeoutObj); clearTimeout(this.serverTimeoutObj); return this; }, start: function(){ var self = this; this.timeoutObj = setTimeout(function(){ //这里发送一个心跳,后端收到后,返回一个心跳消息, //onmessage拿到返回的心跳就说明连接正常 ws.send("HeartBeat"); self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了 ws.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次 }, self.timeout) }, this.timeout) } }
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2019-09-10
m
mr.
您的问题解决了吗,我搭建的是TCP服务器,运行一段时间后才会出现zm_deactivate_swoole (ERROR 9003)。
赞
0
回复
微信公众号
热门内容
暂无回复的问答
- 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一直是同一个。没用使用到多进程啊。