Node.js - Socket.io repeats the 'io.on('connection') over and over

后端 未结 1 1989
野的像风
野的像风 2021-01-28 11:19

So, I am still in the experimental phase of Socket.io, but I just can\'t figure out why my code is doing this. So, I have the code below and when I console.log the code, it repe

相关标签:
1条回答
  • 2021-01-28 11:30

    Here are some of the possible reasons for socket.io connecting over and over:

    1. Your socket.io client and server versions do not match and this causes a connection failure and an immediate retry.

    2. You are running with some infrastructure (like a proxy or load balancer) that is not configured properly to allow lasting webSocket connections.

    3. You are running a clustered server without sticky webSocket connections.

    4. You have put the server-side io.on('connnection', ...) code inside some other function that is called more than once causing you to register multiple event handlers for the same event so you think you're getting multiple events, but actually you just have multiple listeners for the one occurrence of the event.

    5. Your client code is calling its var socket = io() more than once.

    6. Your client page is reloading (and thus restarting the connection on each reload) either because of a form post or for some other reason.

    FYI, you can sometimes learn something useful by installing listeners for all the possible error-related events on both client and server connections and then logging which ones occur and any parameters that they offer. You can see all the client-related error events you can listen to and log here.

    0 讨论(0)
提交回复
热议问题