65928

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

<h3>Question</h3>

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 repeats the the connection even when there is only one connection. Do you know a solution?

io.on('connnection', (socket) => { console.log("A new user is connected.") })

Client side:

<script src="/socket.io/socket.io.js"></script> <script> var socket = io() </script>

Node.js Console:

A new user is connected. A new user is connected. A new user is connected. A new user is connected. A new user is connected. A new user is connected. A new user is connected. ...

(Note: there is only one connection, and I have already cleared the browser cashe)


<h3>Answer1:</h3>

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

<ol><li>

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

</li> <li>

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

</li> <li>

You are running a clustered server without sticky webSocket connections.

</li> <li>

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.

</li> <li>

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

</li> <li>

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

</li> </ol>

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.

来源:https://stackoverflow.com/questions/61030515/node-js-socket-io-repeats-the-io-onconnection-over-and-over

Recommend

  • manipulate delineation of geom_bar and coord_polar in ggplot2 r
  • Is it possible to erase elements of a std::list in a c++11 for each loop
  • PyQt: connecting slots with custom objects via Qt Designer
  • Get Table Cell Values Google Docs
  • HTML5 Video Play/Pause issue
  • ng-repeat in repeat does not repeat nested object angular
  • How to download bytea column as file using Java
  • How to use InjectTouchInput for specific window?
  • Downgrade NG2 Directive to AngularJS
  • Multiple custom authentication with spring security
  • NHibernate - Paging with ICriteria and optional ICriteria calls
  • Find string between two substrings AND between string and the end of file
  • Pyinstaller GLIBC_2.15 not found
  • MySQL - Filter records which date is biggest
  • How to adapt DirectX-style world/view/projection matrices to OpenGL?
  • Laravel 5.7: Custom blade template for Maintenance Mode but not 503.blade.php
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • async GET request with body from browser
  • Do iTunesConnect external testers get updates for *every* uploaded ipa file after Beta Approval?
  • Authorize Attribute Authentication with Postman in Web Api
  • Get name of days between two date in ios?
  • Exclude Classes from Being Included in Shaded Jar
  • Swift manually rotate view controller
  • Arraylist of strings into one comma separated string
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • How to use array in autohotkey?
  • How to make Rss News Reader application in android …? [closed]
  • 'Edit' function for forum posts and such
  • Generate and export point cloud from Project Tango
  • time column in sqlite using gorm
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • How to write seo friendly url's using htaccess?
  • Calculate time from document
  • How to turn off notice reporting in xampp?
  • jQuery scrollTop if URL has hash
  • How to handle div that is created dynamically in a table
  • Computing the discrete fourier transform of audio data with FFTW
  • VLOOKUP in IMPORTRANGE
  • Write to .csv file with PHP (Commas in Data Error)