socket.io creates one more connection after reconnecting

前端 未结 2 1844
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-08 23:55

I am trying a simple chat application here with socket.io and node.js. Every time I restart the node.js server, socket.io automatically reconnects and somehow creates one mo

2条回答
  •  盖世英雄少女心
    2020-12-09 00:42

    Try to write the index.html in this way instead:

     iosocket.on('connect', function () {
          $('#incomingChatMessages').append($('
  • Connected
  • ')); }); iosocket.on('message', function(message) { $('#incomingChatMessages').append($('
  • ').text(message)); }); iosocket.on('disconnect', function() { $('#incomingChatMessages').append('
  • Disconnected
  • '); });

    It may be caused by the other 2 eventlisteners was registered to the connect event. When the client disconnected, the other 2 listeners will still here and doesn't get unregistered.

提交回复
热议问题