简体   繁体   中英

Node.js/Socket.io: Create a bot who's always in chat lobby/room, detecting specific activity and reporting to server?

I am running Node.js and Socket.io on Linux Server. I've created an online chat room/lobby, I am wondering how can I create like a daemon who has a role of user, he is connected to lobby always and is monitoring lobby, and messages that users post. When specific criteria is matched - report back to server.

For example, some user posts too many messages per minute, the bot would send message to user saying to slow down, if user continues bot would send request to server saying to kick that user.

I am new to node.js and socket.io, so I am not sure how to implement it. I don't want to hard code every rule or criteria into server itself.

I don't think your way will be working. The way on top of my head is doing it on server. Socket.io can fire any event on client side and send to server, so you can ask server to listen to certain event and handle your logics accordingly on server. Below is some sample code for your reference.

client side: this event will fire whenever client sends a message.

$("#msgbutton").click(function(){            
        socket.emit("message","some message client send");
    });

server side:

socket.on('message', function(msg){
    var now = new Date();
    var lastsent = socket.lastsent;  //socket is an object and you can store lastsent datetime to it
    var diff = now.getTime() - socket.lastsent.getTime();
    if (diff/1000 > 2)  // if message interval is larger than 2 seconds
    {socket.to(room).emit('message',msg); // send message to whole room}
    else if
    { // maybe send a warning event to user }
});

The code is not tested, and only consider the time difference between current msg and last message. If you want to monitor the message sent event over certain time course, you will have to write your logics on server to do that. Hope this can give you some pointers.

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