doujianmin6527 2018-09-03 16:36
浏览 1173

棘轮websocket onopen事件没有触发,但相同的代码在服务器上工作

Same code working at server but not at local. Though the addEventListener is adding the open event but onopen event is not triggered. Both my local and live server is nginx. Both local and live php version is php7-fpm. The only difference is my server is configured for https but my local is http.

my javascript code:

'use strict';
var servers = {
    iceServers: [
        {urls: 'stun:stun.l.google.com:19302'}
    ]
};

var myPC;
var awaitingResponse;
var streamConstraints;
var myMediaStream;

const room = getRoom();
//const wsChat = new WebSocket("wss://localhost/websocket"); //this is for server which is HTTPS
const wsChat = new WebSocket("ws://localhost:8090");//localhost which is HTTP

wsChat.addEventListener('open', function (event) {
    console.log('open event listener');//getting this on console
});

wsChat.addEventListener('message', function (event) {
    console.log('Message event listener');
});

window.addEventListener('load', function(){    
    console.log(wsChat);
    
    wsChat.onopen = function(event){
        alert('connected'); //not getting this alert
        wsChat.send(JSON.stringify({
            action: 'subscribe',
            room: room
        }));
        showSnackBar("Connected to the chat server!", 5000);
    };
    wsChat.onerror = function(event){
        showSnackBar("Unable to connect to the chat server! Kindly refresh", 10000);
    };    
    wsChat.onmessage = function(e){
        var data = JSON.parse(e.data);
        console.log(data);
        
        if(data.room === room){
            switch(data.action){
                .....
            }  
        }
    };
});

The nginx configuration of my server and my local.

Server: mysite.conf

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    server 127.0.0.1:8090;
}
......
location = /websocket{
    proxy_redirect off;

    proxy_pass http://websocket;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

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

This configuration is working at my live site.

Local nginx conf:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}
upstream websocket {
    server 127.0.0.1:8090;
}
......
location = /websocket {
    proxy_redirect off;

    proxy_pass http://websocket;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_read_timeout 3600;
}

The response what I'm getting at my console for local.

enter image description here

Not getting any clue.

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料