多次触发 Socket.io 消息事件 [英] Socket.io message event firing multiple times

查看:48
本文介绍了多次触发 Socket.io 消息事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习 node 并开始使用 socket.io 创建 mashup消息传输已经开始,但我遇到了一些麻烦.

I was trying to learn node and started creating a mashup with socket.io The message transportation have begin but I have run into some trouble.

消息事件多次触发,导致一条消息多次出现在收件人的邮箱中.我已将套接字路由到 exports.chat 并想知道这是否导致了问题?

The message event is firing multiple times leading to a single message appearing multiple times on the recipient's box. I have routed the socket to exports.chat and was wondering if that is causing the problem?

缩小问题的范围:消息触发的次数=客户端的连接顺序.也就是说,如果客户端第二次连接,他的消息将触发两次.客户端连接第三次三次.

To narrow down the problem: the messages are firing the number of times = the sequence of connection of the client. That is, if a client connects second, his messages will fire twice. three times for the client connecting third.

这是代码片段:

Here is the code snippet:

exports.chat = function(io, pseudoArray, req, res){
    res.render('chat', {title: 'ChatPanel.'});

        var users = 0; 

        io.sockets.on('connection', function (socket) { // First connection
            users += 1; 
        //  reloadUsers(io, users); 

            socket.on('message', function (data) { // Broadcast the message to all
                if(pseudoSet(socket)) {
                    var transmit = {date : new Date().toISOString(), pseudo : returnPseudo(socket), message : data};
                    socket.broadcast.emit('message', transmit);
                    console.log("user "+ transmit['pseudo'] +" said ""+data+""");
                }
            });

            socket.set('pseudo', req.session.user, function(){
                pseudoArray.push(req.session.user);
                socket.emit('pseudoStatus', 'ok');
                console.log("user " + req.session.user + " connected");
            });

            socket.on('disconnect', function () { // Disconnection of the client
                users -= 1;
            //  reloadUsers();
                if (pseudoSet(socket)) {
                    var pseudo;
                    socket.get('pseudo', function(err, name) {
                        pseudo = name;
                    });
                    var index = pseudoArray.indexOf(pseudo);
                    pseudo.slice(index - 1, 1);
                }
            });
        });
};

推荐答案

socket.io 代码的整个部分都必须在 external.chat 函数之外.Socket IO 必须与 http/app 服务器绑定,你不应该在每个请求中处理它.

The whole part of socket.io code has to go outside external.chat function. Socket IO has to bind with the http/app server, you should not handle it within each request.

消息触发的次数=客户端的连接顺序

the messages are firing the number of times = the sequence of connection of the client

本质上发生的事情是,每次有新请求到达时,您都在为消息注册一个事件处理程序,因此它被触发的次数与您访问聊天 URL 的次数相同.

What essentially happening is, each time a new request arrives you are registering a event handler for message, hence it is fired as many times as the you have accessed chat URL.

io.socket.on('message', function (data) {...})

这篇关于多次触发 Socket.io 消息事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆