简体   繁体   中英

ibm bluemix nodejs and websockets keep closing on the client side

Within IBM bluemix, my node clients keep having their websocket connections closed even though my server code does not initiate any closures.

My server side code is as follows:

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);
            }
        }
    });
});

My client side websocket related code is as follows:

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));
            }
        }
    }

Any clues as to what is closing my web sockets?

Thanks, Aaron

After some research, I found that IBM bluemix closes connections every 2 minutes. A security standard was implemented. To solve the problem, I reopen the websocket from the client every 5 minutes and catch an client side closures with a reopen.

//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);
        }

Cheers, Aaron

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM