javascript - ibm bluemix nodejs 和 websockets 在客户端持续关闭

标签 javascript node.js websocket ibm-cloud

在 IBM bluemix 中,即使我的服务器代码未启动任何关闭,我的 Node 客户端仍会关闭其 Websocket 连接。

我的服务器端代码如下:

app.ws('/problemUpdate', function(ws, req) {
    // if we have the maximum number of clients, remove the oldest
    if (clients.length>MAX_CLIENTS){
        ws=clients.pop();
        ws.close();
    }
    clients.unshift(ws);

    ws.on('close', function(msg) {
        // on close, remove clients
        for (var i = 0; i < clients.length; i++) {
            if(clients[i]==ws){
                console.log("removing");
                clients.splice(i,1);
            }
        }
    });
    ws.on('message', function(msg) {
        if (readyToSend(ws)){
            ws.send(ws); 
        }
    });
    // listen the event
    eventEmitter.on('updateProblem', function(updatedProblem){
        //Broadcast to all clients
        console.log("Total Clients: "+clients.length);
        for (var i = 0; i < clients.length; i++) {
            var client = clients[i];
            if (readyToSend(client)){
                client.send(updatedProblem);
            }
        }
    });
});

我的客户端websocket相关代码如下:

updateWebsocket(webSocketProblemUpdate);

    function updateWebsocket(socket){
        socket.onopen = function(){
            console.log("Connection Opened");
        }
        socket.onclose = function(){

        }
        socket.onerror = function(evt){
            console.log("The following error occurred: " + evt.data);
        }
        socket.onmessage = function(evt){

            var jsonProblem = JSON.parse(evt.data);
            var problemName = jsonProblem.envelope.problemName;
            delete jsonProblem["envelope"];
            var option = document.createElement('option');
            option.text=problemName;
            option.value=problemName;
            var alreadyAdded=false;
            [].forEach.call(  document.getElementById('problems')  , function(elm){
                if(elm.value==option.value && elm.text==option.text){
                //if(elm.text==option.text){
                    alreadyAdded=true;
                    // update the content of an already added scenario
                    accumulatedJsonProblems[problemName]=JSON.stringify(jsonProblem);
                    $('.problems').change();
                }
            })
            if (!alreadyAdded){
              accumulatedJsonProblems[problemName]=JSON.stringify(jsonProblem);
              var select = $("#problems")[0];
              select.add(option,$('#problems').children('option').length);
              document.getElementById('problems').value=problemName;
               $('.problems').change();
              console.log("The following data was received:" + JSON.stringify(jsonProblem));
            }
        }
    }

关于什么正在关闭我的网络套接字的任何线索?

谢谢, 亚伦

最佳答案

经过一些研究,我发现 IBM bluemix 每 2 分钟关闭一次连接。实现了安全标准。为了解决这个问题,我每 5 分钟从客户端重新打开一次 websocket,并通过重新打开捕获客户端关闭。

//refresh the websocket every 5 minutes
    setInterval(function() {
        console.log("Interval expired, refreshing websocket");
        // only closing because the on close method automatically opens a new websocket
        webSocketProblemUpdate.close();
    }, 300000); 

socket.onclose = function(){
            console.log("Connection Closed");
            window.WebSocket = window.WebSocket || window.MozWebSocket;
            webSocketProblemUpdate = new WebSocket("ws://"+window.document.location.host+"/problemUpdate");
            updateWebsocket(webSocketProblemUpdate);
        }

干杯, 亚伦

关于javascript - ibm bluemix nodejs 和 websockets 在客户端持续关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928660/

相关文章:

javascript - 将 Angular 表达式传递给函数

javascript - 以编程方式选择 django-autocomplete-light 中的第一个建议

javascript - 在javascript中同步进行地理定位调用

node.js - 格式化 Node 应用程序以进行 AWS 部署

python - 使用 WebRTC 和 Python 服务器进行人脸情绪分析

javascript - 将字符串作为变量传递时,Array.indexOf() 找不到值?

node.js - 默认值在 swagger Node 请求中显示在哪里?

javascript - 如何在静态 Javascript 中嵌入 EJS 代码?

JAVAX JSR 356 Websocket 与 Spring Boot

javascript - 无法从 JavaScript 客户端通过 https 连接到 websocket 服务器