首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
swoole 不支持websocket协议吗
我phpinfo 确认了swoole已经正常安装 服务端 {{{ $ws = new swoole_websocket_server("0.0.0.0", 9502); $ws->on('open', function ($ws, $request) { echo "hello, welcome\n"; }); $ws->on('message', function ($ws, $frame) { echo "Received message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); $ws->start(); }}} 然后PHP 命令运行 客户端 {{{ <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <script> // 创建一个Socket实例 var socket = new WebSocket('ws://121.40.145.240:9502'); // 打开Socket socket.onopen = function(event) { // 发送一个初始化消息 socket.send('I am the client and I\'m listening!'); // 监听消息 socket.onmessage = function(event) { console.log('Client received a message',event); }; // 监听Socket的关闭 socket.onclose = function(event) { console.log('Client notified socket has closed',event); }; // 关闭Socket.... //socket.close() }; </script> <body> </body> </html> }}} 运行客户端直接报错 WebSocket connection to 'ws://121.40.145.240:9502/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
发布于10年前 · 1 次浏览 · 来自
提问
我
我吃不饱
我phpinfo 确认了swoole已经正常安装 服务端 {{{ $ws = new swoole_websocket_server("0.0.0.0", 9502); $ws->on('open', function ($ws, $request) { echo "hello, welcome\n"; }); $ws->on('message', function ($ws, $frame) { echo "Received message: {$frame->data}\n"; $ws->push($frame->fd, "server: {$frame->data}"); }); $ws->start(); }}} 然后PHP 命令运行 客户端 {{{ <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <script> // 创建一个Socket实例 var socket = new WebSocket('ws://121.40.145.240:9502'); // 打开Socket socket.onopen = function(event) { // 发送一个初始化消息 socket.send('I am the client and I\'m listening!'); // 监听消息 socket.onmessage = function(event) { console.log('Client received a message',event); }; // 监听Socket的关闭 socket.onclose = function(event) { console.log('Client notified socket has closed',event); }; // 关闭Socket.... //socket.close() }; </script> <body> </body> </html> }}} 运行客户端直接报错 WebSocket connection to 'ws://121.40.145.240:9502/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2016-08-17
涅
涅×磐
没人?
赞
0
回复
2016-09-25
4
468
估计和你的环境有关,猜测是防火墙 我的系统完全正常 在 WINDOWS 下面双击 HTML 文件 浏览器打开,然后看到 LINUX 服务器端输出如下: [root@server ~]# php a.php hello, welcome Received message: I am the client and I'm listening!
赞
0
回复
2017-02-06
d
dapengtongfeng
检查下防火墙对端口有没有限制吧
赞
0
回复
2019-09-25
N
NicholasFugui
我也遇到这个问题,目前已解决: 1.检查防火墙对外开放端口,确认9502是否开启 查看开启端口列表centos7:firewall-cmd --zone=public --list-ports 开启端口:firewall-cmd --zone=public --add-port=9502/tcp 2. 如果是阿里云服务器,需要对9502设置相应的“安全组策略” 希望能帮助到你
赞
0
回复