GO websocket搭建,https环境

2/22/2017来源:ASP.NET技巧人气:2521

beego 框架搭建websocket聊天室,本地是80端口测试,没有考虑线上https情况,当部署到线上机器,发现https下websocket连接不上,需要进行修改,故进行记录,希望遇到的同学,可以少踩坑

js websocket连接方法

function connect() {
       ws = new WebSocket("wss://"+document.location.host+"/ws");
       ws.onopen = onopen;
       ws.onmessage = onmessage; 
       ws.onclose = function() {
          console.log("连接关闭,定时重连");
          connect();
       };
       ws.onerror = function() {
          console.log("出现错误");
       };
    }


nginx配置websocket支持

        location /ws {
            PRoxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_redirect off;
            proxy_pass http://127.0.0.1:自己的端口/ws;
        }


最终效果如图