簡體   English   中英

ibm bluemix nodejs和websockets在客戶端保持關閉

[英]ibm bluemix nodejs and websockets keep closing on the client side

在IBM bluemix中,即使我的服務器代碼未啟動任何關閉,我的節點客戶端仍保持其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);
        }

干杯,亞倫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM