为什么我的 Socket.io 事件会触发多次?
Why is my Socket.io event firing multiple times?
我正在与 Socket.io 建立一个简单的聊天室。用户输入所需的用户名并提交表单。
我检查用户名是否正在使用,然后在用户名可用时触发 join
事件,这会将他们添加到聊天中,但 join
事件似乎是针对每个事件触发的当前在聊天中的用户 - 不仅仅是新用户进入一次。
客户端
var socket = io();
// user enters desired username and submits form
$('#chat__join').on('submit', function() {
// get username value
var name = $('#chat__name').val();
// send socket username to check if someone is already using it
socket.emit('isUserInChat', name);
return false;
});
// callback for when we know if user is already in chat or not
socket.on('isUserInChat', function(exists, name) {
if(!exists) {
// this only fires once per form submission
console.log('username is available');
// but this fires per user
socket.emit("join", name);
} else {
// username is taken
// display error message
}
});
socket.emit("join", name);
行似乎在第一个人提交表单时触发一次,这是正确的。但是当第二个人提交表单时,join
事件被触发两次。当第三人提交表单时,它会被触发三次。为什么每个用户都会触发事件?它不应该只在每次提交表单时触发吗?
服务器
var express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
usersObj = {
users: []
};
io.on('connection', function(socket) {
socket.on('isUserInChat', function(username) {
var flag = false;
// for each user
for(var i = 0; i < usersObj.users.length; i++) {
// if username matches username that was entered
if(usersObj.users[i].username == username) {
// set flag to be true
flag = true;
}
}
// send client true or false
io.emit('isUserInChat', flag, username);
});
socket.on('join', function(name) {
// construct user
var user = {
id: socket.id,
username: name
};
// add user to users object array
usersObj.users.push(user);
// send chat message
io.emit('sendMsg', 'Chat Bot', name+' has connected to the server.');
});
});
你看起来很困惑,但你实际上告诉事件要发送给每个客户端。如果您希望它仅回显给正在检查可用性的客户端
io.emit('isUserInChat', flag, username);
应该是
socket.emit('isUserInChat', flag, username);
我正在与 Socket.io 建立一个简单的聊天室。用户输入所需的用户名并提交表单。
我检查用户名是否正在使用,然后在用户名可用时触发 join
事件,这会将他们添加到聊天中,但 join
事件似乎是针对每个事件触发的当前在聊天中的用户 - 不仅仅是新用户进入一次。
客户端
var socket = io();
// user enters desired username and submits form
$('#chat__join').on('submit', function() {
// get username value
var name = $('#chat__name').val();
// send socket username to check if someone is already using it
socket.emit('isUserInChat', name);
return false;
});
// callback for when we know if user is already in chat or not
socket.on('isUserInChat', function(exists, name) {
if(!exists) {
// this only fires once per form submission
console.log('username is available');
// but this fires per user
socket.emit("join", name);
} else {
// username is taken
// display error message
}
});
socket.emit("join", name);
行似乎在第一个人提交表单时触发一次,这是正确的。但是当第二个人提交表单时,join
事件被触发两次。当第三人提交表单时,它会被触发三次。为什么每个用户都会触发事件?它不应该只在每次提交表单时触发吗?
服务器
var express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http),
usersObj = {
users: []
};
io.on('connection', function(socket) {
socket.on('isUserInChat', function(username) {
var flag = false;
// for each user
for(var i = 0; i < usersObj.users.length; i++) {
// if username matches username that was entered
if(usersObj.users[i].username == username) {
// set flag to be true
flag = true;
}
}
// send client true or false
io.emit('isUserInChat', flag, username);
});
socket.on('join', function(name) {
// construct user
var user = {
id: socket.id,
username: name
};
// add user to users object array
usersObj.users.push(user);
// send chat message
io.emit('sendMsg', 'Chat Bot', name+' has connected to the server.');
});
});
你看起来很困惑,但你实际上告诉事件要发送给每个客户端。如果您希望它仅回显给正在检查可用性的客户端
io.emit('isUserInChat', flag, username);
应该是
socket.emit('isUserInChat', flag, username);