是否可以仅向发送者全局发出但在 socket.on('connect') 事件之外?我正在寻找socket.broadcast.to().emit()
可以在 app.post 方法中使用的东西,而不是在 connect 方法中,所以我不能只使用 socket.emit
app.post('/room', function(req, res) {
io.sockets.emit('something');
});
'东西' 我只想发送给 SENDER
我的代码:
socket_id;
app.post('/room', function(req, res) {
if (req.body.user == ''){
io.sockets.socket(socket_id).emit('usernameError', 'Type your username');
}
else if (users[req.body.user] !== undefined ) {
io.sockets.socket(socket_id).emit('usernameError', 'Username exists.');
}
else{
users[req.body.user] = req.body.user;
user = req.body.user;
io.sockets.emit('newUser', user+' join to the room');
res.render('room');
}
});
io.sockets.on('connection', function(socket) {
socket.username = user;
socket.join('room1');
socket.id = socket_id;
});