简体   繁体   English

Node.js / Socket.io:创建一个始终在聊天大厅/房间中,检测特定活动并向服务器报告的机器人?

[英]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. 我在Linux服务器上运行Node.js和Socket.io。 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. 我是node.js和socket.io的新手,所以我不确定如何实现它。 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. Socket.io可以在客户端触发任何事件并将其发送到服务器,因此您可以要求服务器侦听某些事件并在服务器上相应地处理逻辑。 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. 希望这可以给您一些指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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