How to handle when socket.io event is unknown

I have a server side of the code.

var io = require('socket.io').listen(8080); io.sockets.on('connection', function (socket) { socket.on('event', function(data){ // process... }); }); 

client side (part)

 socket.emit('event', {type:'command', data:'data....'}); for (var i=0; i<=9999999; i++){ socket.emit('unknownEvent', {'type':'attack', data:'a34tagfa3wtasfd'}); } 

Now, how to handle and protect the event "unknownEvent" ...? ..

+4
source share
3 answers

An older question, but he came across a similar case, so I decided that I would share how to do it.

Client side

Therefore, instead of passing an event on the client, use the common name of the event, such as event or something else, as the first argument to emit. I ended up using the application name since it was short. Then in the data object, pass the name of the function you want to call.

So this is:

 socket.emit('awesomeSocketEvent', {hello: 'world'}); 

It becomes:

 socket.emit('genericEvent', {event:'awesomeSocketEvent', hello:'world'}); 

The rewriting of everything on the client side sucks, and my client stuff was basically just emit , so I wrapped the socket.io client in another object with its own emit event, which changed the event names and data objects.

Server side

Now on the server side there will always be only one custom event, genericEvent , so custom events should be restructured as properties of the object. Once this happens, this is a simple check to see if an event exists in the object, call it if it exists, or do something else if it isn't.

 io.sockets.on('connection', function (socket) { var events = { awesomeSocketEvent: function(data){ // do your socket stuff here } }; socket.on('genericEvent', function(data){ if(data.event in events){ events[data.event](data); } else { // do your unknown event stuff here } }); }); 

After looking at the code while writing this part, I'm not sure about the performance with the fact that the event object is declared in the connect event. It might be better to declare events outside the connection event, and then pass socket to all of your functions.

+2
source

As Gntem said, if the Socket.io server does not have a registered event handler, the event will simply return and will not process anything.

If you are looking for additional protection (you expect someone to spam you with unregistered events, for example), you might consider using middleware that checks to see if the name of the event is valid (or checks other qualities). Something like that:

 const socketChecker = { someEvent:true, } io.on('connection', function(socket) { socket.use((packet, next) => { if(socketChecker[packet[0]] === true){ next() } else { //Disconnect user here } }); socket.on('someEvent',function(){ //do work here }) }); 

Please note: if you do this, you are reading packages that may have reserved names (e.g. heartbeat, connect, message, json, etc.).

Here's the link for the packages: http://gevent-socketio.readthedocs.io/en/latest/packet.html

Please note that I do not actually use this, so I have no idea what problems might arise. I just checked where the event name is located in some packages. More documentation (or more testing) needs to be found on what is going on inside these packages.

0
source

if the client side or the server side of Socket.io do not have registered handlers for this event, then it will not process it, that is, it will return and do something.

But in case the client sends an unregistered event to the server, you can always use the message event on the client side or on the server side for each message received.

 socket.on('message',function(m){console.log(m);}); 
-one
source

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


All Articles