简体   繁体   中英

How do you stop client-side Socket.io from looping socket emissions?

Let's say I have a socket.io event called 'add' which takes 0 parameters and when received on the server side, increments a count variable and then emits the new count value to every client. How can I stop people from essentially opening up the JS console and using a loop like:

for(var i = 0; i < 1000; i++) {
    socket.emit('add');
}

Another scenario which may make more sense is: how can I detect flooding in, say, a chatting application?

I fully understand that there's nothing one can really do to ensure that client side code isn't manipulated by user.

How would one go about adding some form of timeout for each socketio event handler for each client?

Just for validation :

why not augmenting the client with lasttime = Date.now() and check if lasttime - Date.now() > fixedtimeout

or :

using a key val array to store client addresses along with previous message time and then checking if the difference between the time was less than 100ms

:-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM