首页
下载
文档
问答社区
视频
捐赠
源代码
AI 助理
赞助商
CRMEB
Apipost
腾讯云
微擎
禅道
51Talk
商业产品
Swoole AI 智能文档翻译器
Swoole-Compiler PHP 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
刚学swoole 就遇到WebSocket 服务端跟客户端一直链接不成功
### 问题描述 ### Swoole版本,PHP版本,以及操作系统版本信息 Swoole 4.3 PHP7.3 CenterOS 7+ ### 相关代码 服务端代码 ```php <?php class WebSocket { private $ws = null; public function __construct(){ //创建WebSocket Server对象,监听0.0.0.0:9502端口 $this -> ws = new Swoole\WebSocket\Server('0.0.0.0', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL); $this -> ws->set( [ 'ssl_cert_file' => '__DIR__/fullchain.pem', 'ssl_key_file' => '__DIR__/privkey.pem', ] ); //监听WebSocket连接打开事件 $this -> ws ->on('Open', [$this, "onOpen"]); //监听WebSocket消息事件 $this -> ws->on('Message', [$this, "onMessage"]); //监听WebSocket连接关闭事件 $this -> ws->on('Close', [$this, "onClose"]); $this -> ws->start(); } public function onOpen($ws, $request){ var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "欢迎客户端: {$request -> fd}\n"); } public function onMessage($ws, $frame){ echo "信息: {$frame->data}\n"; foreach ($ws -> connections as $fd){ if($fd == $frame -> fd){ $ws->push($fd, "我: {$frame->data}"); }else { $ws->push($fd, "对方: {$frame->data}"); } } } public function onClose($ws, $fd){ echo "客户端:{$fd} 关闭\n"; } } new WebSocket(); ``` 前端代码 ```html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="welcome"></div> <div> <input type="text" id="input" /> <input type="button" onclick="send()" value="发送"/> </div> <div id="message"></div> <script src="./jquery-1.9.js"></script> <script> var wsServer = 'wss://zyhahaha.com:9501'; var websocket = new WebSocket(wsServer); console.log(websocket); websocket.onopen = function (res) { $("#welcome").append( "<h1>链接成功,欢迎!</h1>" ); }; websocket.onclose = function (res) { $("#message").append( "<h3>链接关闭</h3>" ); }; websocket.onmessage = function (res) { $("#message").append( "<h3>" + res.data + "<h3>" ); }; websocket.onerror = function (res, e) { $("#message").append( "<h3>" + res.data + "<h3>" ); }; function send(){ websocket.send($("#input").val()); } </script> </body> </html> ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 有没有大兄弟能指点一下我
发布于2年前 · 23 次浏览 · 来自
提问
xu007
### 问题描述 ### Swoole版本,PHP版本,以及操作系统版本信息 Swoole 4.3 PHP7.3 CenterOS 7+ ### 相关代码 服务端代码 ```php <?php class WebSocket { private $ws = null; public function __construct(){ //创建WebSocket Server对象,监听0.0.0.0:9502端口 $this -> ws = new Swoole\WebSocket\Server('0.0.0.0', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL); $this -> ws->set( [ 'ssl_cert_file' => '__DIR__/fullchain.pem', 'ssl_key_file' => '__DIR__/privkey.pem', ] ); //监听WebSocket连接打开事件 $this -> ws ->on('Open', [$this, "onOpen"]); //监听WebSocket消息事件 $this -> ws->on('Message', [$this, "onMessage"]); //监听WebSocket连接关闭事件 $this -> ws->on('Close', [$this, "onClose"]); $this -> ws->start(); } public function onOpen($ws, $request){ var_dump($request->fd, $request->get, $request->server); $ws->push($request->fd, "欢迎客户端: {$request -> fd}\n"); } public function onMessage($ws, $frame){ echo "信息: {$frame->data}\n"; foreach ($ws -> connections as $fd){ if($fd == $frame -> fd){ $ws->push($fd, "我: {$frame->data}"); }else { $ws->push($fd, "对方: {$frame->data}"); } } } public function onClose($ws, $fd){ echo "客户端:{$fd} 关闭\n"; } } new WebSocket(); ``` 前端代码 ```html <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div id="welcome"></div> <div> <input type="text" id="input" /> <input type="button" onclick="send()" value="发送"/> </div> <div id="message"></div> <script src="./jquery-1.9.js"></script> <script> var wsServer = 'wss://zyhahaha.com:9501'; var websocket = new WebSocket(wsServer); console.log(websocket); websocket.onopen = function (res) { $("#welcome").append( "<h1>链接成功,欢迎!</h1>" ); }; websocket.onclose = function (res) { $("#message").append( "<h3>链接关闭</h3>" ); }; websocket.onmessage = function (res) { $("#message").append( "<h3>" + res.data + "<h3>" ); }; websocket.onerror = function (res, e) { $("#message").append( "<h3>" + res.data + "<h3>" ); }; function send(){ websocket.send($("#input").val()); } </script> </body> </html> ``` ### 你期待的结果是什么?实际看到的错误信息又是什么? 有没有大兄弟能指点一下我
赞
0
分享
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
评论
2021-12-03
北巷
看看js 报什么错? 看看nginx 配置?
赞
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一直是同一个。没用使用到多进程啊。