Node.js 套接字事件之间的差异
以下两者之间的区别是什么:Node.js 套接字事件之间的差异,node.js,sockets,socket.io,Node.js,Sockets,Socket.io,以下两者之间的区别是什么: io.sockets.socket(); io.sockets.emit(); io.sockets.on(); sockets.on(); socket.broadcast.emit(); socket.emit(); 谁能给我解释一下这两者的区别吗?我知道,sockets.on();是一个事件侦听器,但我不知道区别 而且。。。如何将这两行合并为一行 socket.on('msg_user', function(usr, username, msg) {
io.sockets.socket();
io.sockets.emit();
io.sockets.on();
sockets.on();
socket.broadcast.emit();
socket.emit();
谁能给我解释一下这两者的区别吗?我知道,sockets.on();是一个事件侦听器,但我不知道区别
而且。。。如何将这两行合并为一行
socket.on('msg_user', function(usr, username, msg) {
io.sockets.socket(usernames[usr]).emit('msg_user_handle', username, msg);
io.sockets.socket(usernames[username]).emit('msg_user_handle', username, msg);
});
我可以使用io.sockets.emit();但我不想在所有连接的套接字中发射,只在消息发送方和接收方上发射
这是我的整个应用程序
app.js
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(8008);
// usernames which are currently connected to the chat
//var usernames = {};
var usernames = new Object();
function check_key(v) {
var val = '';
for(var key in usernames) {
if(usernames[key] == v)
val = key;
}
return val;
}
io.sockets.on('connection', function (socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = socket.id;
// echo to client they've connected
socket.emit('updatechat', 'server', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected: ' + socket.id);
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
// when the user sends a private msg to a user id, first find the username
socket.on('check_user', function(asker, id){
io.sockets.socket(usernames[asker]).emit('msg_user_found', check_key(id));
});
// when the user sends a private message to a user.. perform this
socket.on('msg_user', function(usr, username, msg) {
io.sockets.socket(usernames[usr]).emit('msg_user_handle', username, msg);
io.sockets.socket(usernames[username]).emit('msg_user_handle', username, msg);
});
});
var socket = io.connect('http://localhost:8008');
//set username
var my_username = $("#user_data_for_mess").attr("my_username");
var username = $("#user_data_for_mess").attr("username");
// on connection to server, send user's name with an anonymous callback
socket.on('connect', function() {
// call the server-side function 'adduser' and send one parameter (my_username)
socket.emit('adduser', my_username);
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data) {
$('#show_conversation').append(data);
});
// listener, whenever the server emits 'msg_user_found'
socket.on('msg_user_found', function (username) {
$('#message').keypress(function(e) {
if(e.which == 13) {
var message = $('#message').val();
if($.trim(message).length != 0) {
socket.emit('msg_user', username, my_username, message);
$('#message').val('');
}
}
});
});
socket.on('updateusers', function(data) {
socket.emit('check_user', my_username, data[username]);
});
// listener, whenever the server emits 'msg_user_handle', this updates the chat body
socket.on('msg_user_handle', function (username, data) {
$('#show_conversation').append(username + ": " + data);
});
client.js
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
server.listen(8008);
// usernames which are currently connected to the chat
//var usernames = {};
var usernames = new Object();
function check_key(v) {
var val = '';
for(var key in usernames) {
if(usernames[key] == v)
val = key;
}
return val;
}
io.sockets.on('connection', function (socket) {
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function(username){
// we store the username in the socket session for this client
socket.username = username;
// add the client's username to the global list
usernames[username] = socket.id;
// echo to client they've connected
socket.emit('updatechat', 'server', 'you have connected');
// echo globally (all clients) that a person has connected
socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected: ' + socket.id);
// update the list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
});
// when the user disconnects.. perform this
socket.on('disconnect', function(){
// remove the username from global usernames list
delete usernames[socket.username];
// update list of users in chat, client-side
io.sockets.emit('updateusers', usernames);
// echo globally that this client has left
socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
});
// when the user sends a private msg to a user id, first find the username
socket.on('check_user', function(asker, id){
io.sockets.socket(usernames[asker]).emit('msg_user_found', check_key(id));
});
// when the user sends a private message to a user.. perform this
socket.on('msg_user', function(usr, username, msg) {
io.sockets.socket(usernames[usr]).emit('msg_user_handle', username, msg);
io.sockets.socket(usernames[username]).emit('msg_user_handle', username, msg);
});
});
var socket = io.connect('http://localhost:8008');
//set username
var my_username = $("#user_data_for_mess").attr("my_username");
var username = $("#user_data_for_mess").attr("username");
// on connection to server, send user's name with an anonymous callback
socket.on('connect', function() {
// call the server-side function 'adduser' and send one parameter (my_username)
socket.emit('adduser', my_username);
});
// listener, whenever the server emits 'updatechat', this updates the chat body
socket.on('updatechat', function (username, data) {
$('#show_conversation').append(data);
});
// listener, whenever the server emits 'msg_user_found'
socket.on('msg_user_found', function (username) {
$('#message').keypress(function(e) {
if(e.which == 13) {
var message = $('#message').val();
if($.trim(message).length != 0) {
socket.emit('msg_user', username, my_username, message);
$('#message').val('');
}
}
});
});
socket.on('updateusers', function(data) {
socket.emit('check_user', my_username, data[username]);
});
// listener, whenever the server emits 'msg_user_handle', this updates the chat body
socket.on('msg_user_handle', function (username, data) {
$('#show_conversation').append(username + ": " + data);
});
您目前解决私人消息传递的方式很好。我也在其他一些项目中使用了它,效果非常好。您可能还可以使用socket.join()解决此问题。谢谢,我如何使用socket.join()?加入“聊天室”很有用,因为你正在聊天()。但是对于私有消息,我认为在这种情况下io.sockets.socket工作得足够好。我想稍后使用sockets实现一个通知系统,我认为这可能会有所帮助。再次感谢:)