zoukankan      html  css  js  c++  java
  • socket.io 由于使用了 Nginx ,所以需要特别配置

    首先上代码

    location ^~/socket.io {
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_pass http://127.0.0.1:7001;
        }
        location ^~/testsocket.io {
            proxy_set_header X-Real_IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X_Forward_For $proxy_add_x_forwarded_for;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
    
            proxy_pass http://127.0.0.1:7001;
        }

    这块我用的是socket.io 这是nginx的配置,大概意思就是使用HTTP1.1协议,遇到websocket协议就转发吧,升级到指定协议

    服务端代码

    server.listen(7001, ( )=>{
        console.log( 'scoket启动:7001' )
    });

    现在两人成功通信

    前端误区 :

      1 http请求升级

        http://test.com/socket.io/?EIO=3&transport=polling&t=MmeguIG

      2 升级成功

        ws://test.com/socket.io/?EIO=3&transport=websocket&sid=C_h-1N6_v4zSsDDPAAAA

      socket.io 每次连接的前缀 /socket.io 所以nginx才会在转发层加上 ~/scoket.io 

    var socket = io();
    // Whenever the server emits 'new message', update the chat body
    socket.on('server message', function (data) {
        console.log( data )
        //data就是"我连接成功了" socket.emit(
    'shoudaomessage', "+++++收到消息+++++"); });

    先写到这,等做出了功能,我再来添加完成版

  • 相关阅读:
    【20170923】2017暑假北京学习 day 3
    Leetcode 39
    事无巨细 | 访问一个网站的过程
    Leetcode 77
    排序 | 快速排序
    排序 | 堆排序
    Leetcode 60
    Leetcode 51
    海量数据处理的解法
    Leetcode 99
  • 原文地址:https://www.cnblogs.com/qkstart/p/11246173.html
Copyright © 2011-2022 走看看