我是靠谱客的博主 顺利口红,这篇文章主要介绍WebSocket connection to 'ws://localhost/aa/ws1' net::ERR_CONNECTION_REFUSED,现在分享给大家,希望可以做个参考。

在项目中使用了websocket 进行数据推送 在本地访问没问题 部署到服务器上之后报 WebSocket connection to 'ws://localhost/aa/ws1'  net::ERR_CONNECTION_REFUSED

很是郁闷!!

后面发现服务器上使用了nginx,发现也需要在nginx中配置websocket

前台

function init(){
    if ('WebSocket' in window) {
        websocket = new WebSocket("ws://localhost/aa/ws1");
    } else if ('MozWebSocket' in window) {
        websocket = new MozWebSocket("ws://localhost/aa/ws1");
    } else {
        websocket = new SockJS("http://localhost/aa/ws1/sockjs");
    }
    websocket.onopen = function() {
        console.log('open');
    }
}

后台

registry.addHandler(new MyHandler(), "/ws1");
registry.addHandler(new MyHandler(), "/ws1/sockjs").addInterceptors(new MyHandlShakeInterceptor()).withSockJS();

nginx配置

upstream test{
    server localhost:8080;   
}
location /aa/ws1 {
    proxy_pass http://test;
    proxy_redirect off;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

 

解决~~

最后

以上就是顺利口红最近收集整理的关于WebSocket connection to 'ws://localhost/aa/ws1' net::ERR_CONNECTION_REFUSED的全部内容,更多相关WebSocket内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部