PHPSocket.IO 怎么使用websocket协议

OneCMS

现在测试的WEB消息推送框架,
可以http协议连接,https:// 连接也可以。
出于其他考虑,需要使用wss:// 连接。(目前直接连接握手失败)
没找到声明的地方,求指教。

2395 6 0
6个回答

计算机爱好者

var socket= io("wss://xxx.xxx.com/wss")
我的是这样连的,你用wss应该是小程序要用到的协议吧

  • 计算机爱好者 2019-11-13

    服务器还有相应的设置,我的是niginx,你可以参考一下我的nginx配置:
    location /socket.io
    {
    proxy_pass http://127.0.0.1:监听的端口;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    }

    如果proxy_pass是https的话就可以在前面加上https,如果是wss,就去掉s改为http,反正连不上就是有s和没s的问题

OneCMS

客户端是没问题的,目前是服务端起不来。

你的不错啊,能不能把nginx 配置全部贴出来。我尝试过用nginx代理,没成功。

  • 暂无评论
OneCMS

单独架设wokerman,不用PHPsocket.io ,然后用$ws_worker = new Worker("websocket://0.0.0.0:2120");就可以使用ws:// 连接 。
但是使用上这个消息推送框架,里边用到了PHPsocket.io ,ws方式连接就不能用了

OneCMS
server 
{
    listen 80;
    listen 443 ssl;  
    server_name cy99.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root C:/wwwroot/m;

    #START-ERROR-PAGE
    #error_page 403 /403.html;
    error_page 404 /404.html;
    #error_page 502 /502.html;
    #END-ERROR-PAGE

    #HTTP_TO_HTTPS_START
    #HTTP_TO_HTTPS_END

    #LIMIT_INFO_START
    #LIMIT_INFO_END

    #SSL-INFO-START
    ssl_certificate    ssl/cy99/fullchain.pem;
    ssl_certificate_key    ssl/cy99/privkey.pem;
    ssl_protocols SSLv3 SSLv2 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri; 
    #SSL-INFO-END

    location /wss
    {
        proxy_pass http://127.0.0.1:2120;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Real-IP $remote_addr;
    } 
}

观摩一下。代理配置。配置应该是ok的

  • 暂无评论
计算机爱好者
server
{
    listen 80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    listen [::]:80;
    server_name xxxxxx.com;
    index index.html index.php index.htm default.php default.htm default.html;
    root /www/wwwroot/xxxxx;

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
    rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate /xxxxxxxxxxxxx
    ssl_certificate_key /xxxxxxxxxxxxxxxxxxxx;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497 https://$host$request_uri;
    location /socket.io
    {
    proxy_pass http://127.0.0.1:3120;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    }
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
    return 404;
    }
}

前段访问:假设自己的域名是百度

var socket= io("wss://www.baidu.com:3120",{ 'reconnect': true });

我是这样配置的

不过因为你的location /wss可能你访问的话

var socket= io("wss://www.baidu.com/wss:3120",{ 'reconnect': true });
  • 暂无评论
walkor

PHPSocket.IO通讯协议是固定的,握手的时候是http握手,然后将连接升级为websocket,通过websocket通讯。
不支持直接用websocket去连接。

  • OneCMS 2019-11-14

    听到你的官方回复,我就放心了。
    关键现在客户端(已封装),不支持使用http连接,只能通过ws或者wss进行连接。求解了

  • OneCMS 2019-11-14

    你说这个PHPSocket.IO,要是能提供一个参数选择,以便用户能自由选择连接协议。会不会更人性化些。

年代过于久远,无法发表回答
🔝