Socket.io client: how to determine if a handler is installed

What happens if you call the 'on' method several times for the same function on the socket? Does it call several times simply on top of the last registered function or use more resources?

If this is later, how do you determine if a handler is registered?

+5
source share
2 answers

I just looked at a socket in Firebug, there is a member called "_allbacks".

It contains all the registered callbacks, so finding, if already registered, is simple as:

if ( socket._callbacks[strHandlerName] == undefined ) { //Handler not present, install now socket.on(strHandlerName, function () { ... } ); } 

Here it is!

+6
source

I'm used to working with him that way.

  var baseSocketOn = socket.on; socket.on = function() { var ignoreEvents = ['connect'] //maybe need it if (socket._callbacks !== undefined && typeof socket._callbacks[arguments[0]] !== 'undefined' && ignoreEvents.indexOf(arguments[0]) === -1) { return; } return baseSocketOn.apply(this, arguments) }; 

This is the best practice.

+1
source

Source: https://habr.com/ru/post/1237973/


All Articles