2016-04-29 6 views
0

IBM bluemix内で、自分のサーバーコードがクロージャを開始しなくても、自分のwebsocket接続を閉じたままにします。ibm bluemix nodejsおよびwebsocketsがクライアント側で閉じる

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

どれでも手がかりを私のウェブソケットを閉じているものに関して、次のよう

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関連のコードは次のように

私のサーバー側のコードはありますか?

おかげで、いくつかの研究の後 アーロン

答えて

0

は、私は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); 
     } 

乾杯、 アーロン

関連する問題