有什么区别:
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);
});
});
client.js
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);
});
答案 0 :(得分:1)
io.sockets.socket() - for emiting to specific clients
io.sockets.emit() - send to all connected clients (same as socket.emit)
io.sockets.on() - initial connection from a client.
socket.on() - event listener, can be called on client to execute on server
socket.emit() - send to all connected clients
socket.broadcast.emit() - send to all connected clients except the one that sent the message
目前解决私人消息的方式很好。我也在其他一些非常好的项目中使用了这个。您也可以使用socket.join()
来解决它