2016-02-24 47 views
0

我正在尝试制作一个聊天应用程序,它将第一条消息发送为“Hi。there”。 但由于某些原因(不稳定的互联网连接可能是一个),套接字会多次初始化并多次发送“Hi。there”消息。以下是代码。如何停止发送多条消息的应用程序?阻止Socket连接再次初始化

io.socket.on('connect', function() { 
     /* On successfull connection to server */ 
     console.log('Connected to server'); 
     //Some code here 
     io.socket.get(url + '/userstatus/subscribe', function(resData, jwres) { 
      //Some code here 
      io.socket.on("userstatus", function(event) { 
        //Socket updartes 
        // Send Message code at this line. 
       } 
      } 
     }); 
+0

你的客户端代码? – sdg

+0

这是客户端代码。 –

回答

0

你需要改变你的客户端代码,因此它存储状态,并将其发送到服务器上重新连接。这样服务器可以给出正确的响应。

像这样的东西可能会奏效:

socket.on('connect', function() { 
    /* On successfull connection to server */ 
    console.log('Connected to server'); 
    socket.emit('state', state_object); 
}); 
+0

这是我的客户端代码,你能提出任何改变吗? –