Javascript 如何使用node.js和socket.io实现像facebook这样的私人聊天
这是我写的代码,但它不能与登录用户聊天 服务器端Javascript 如何使用node.js和socket.io实现像facebook这样的私人聊天,javascript,node.js,sockets,Javascript,Node.js,Sockets,这是我写的代码,但它不能与登录用户聊天 服务器端 var usernames = {}; // rooms which are currently available in chat var rooms = []; io.sockets.on('connection', function (socket) { socket.on('USER_ONLINE', function (data) { rooms.push(data); }); // wh
var usernames = {};
// rooms which are currently available in chat
var rooms = [];
io.sockets.on('connection', function (socket) {
socket.on('USER_ONLINE', function (data) {
rooms.push(data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function (username) {
// store the username in the socket session for this client
socket.username = username;
// store the room name in the socket session for this client
socket.room = 'room1';
// add the client's username to the global list
usernames[username] = username;
// send client to room 1
socket.join('room1');
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected to room');
// echo to room 1 that a person has connected to their room
socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room');
socket.emit('updaterooms', rooms, 'room1');
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (rum,data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.in(socket.room).emit('updatechat', socket.username, data,rum);
});
socket.on('switchRoom', function (newroom) {
socket.leave(socket.room);
socket.join(newroom);
socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
// sent message to OLD room
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
// update socket session room title
socket.room = newroom;
socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
socket.emit('updaterooms', rooms, newroom);
});
// 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');
socket.leave(socket.room);
});
});
var socket = io.connect('http://localhost:8585');
socket.on('connect', function () {
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser', '<?php echo $usermanager_user_company[0]['pc_users']['us_username'];?>');
});
$('#chatmsg').each(function () {
var list = $(this).find('li span').html();
socket.emit('USER_ONLINE',list);
});
socket.on('updatechat', function (username, data,rum) {
$('.'+rum).append('<li><b>'+username + ':</b> ' + data + '</li>');
});
socket.on('updaterooms', function (rooms, current_room) {
socket.emit('switchRoom', current_room);
});
function switchRoom (room) {
//alert(room);
active(room);
}
function active(room) {
//var message2 = $('.'+room).val();
//socket.emit('sendchat', room,message2);
$('.'+room).keydown(function (event) {
if(event.keyCode == 13) {
var message2=$(this).val();
// alert('Hi');
// tell server to execute 'sendchat' and send along one parameter
socket.emit('sendchat',room, message2);
//$('.messages li').last().focus();
}
});
}
客户端
var usernames = {};
// rooms which are currently available in chat
var rooms = [];
io.sockets.on('connection', function (socket) {
socket.on('USER_ONLINE', function (data) {
rooms.push(data);
});
// when the client emits 'adduser', this listens and executes
socket.on('adduser', function (username) {
// store the username in the socket session for this client
socket.username = username;
// store the room name in the socket session for this client
socket.room = 'room1';
// add the client's username to the global list
usernames[username] = username;
// send client to room 1
socket.join('room1');
// echo to client they've connected
socket.emit('updatechat', 'SERVER', 'you have connected to room');
// echo to room 1 that a person has connected to their room
socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room');
socket.emit('updaterooms', rooms, 'room1');
});
// when the client emits 'sendchat', this listens and executes
socket.on('sendchat', function (rum,data) {
// we tell the client to execute 'updatechat' with 2 parameters
io.sockets.in(socket.room).emit('updatechat', socket.username, data,rum);
});
socket.on('switchRoom', function (newroom) {
socket.leave(socket.room);
socket.join(newroom);
socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
// sent message to OLD room
socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
// update socket session room title
socket.room = newroom;
socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
socket.emit('updaterooms', rooms, newroom);
});
// 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');
socket.leave(socket.room);
});
});
var socket = io.connect('http://localhost:8585');
socket.on('connect', function () {
// call the server-side function 'adduser' and send one parameter (value of prompt)
socket.emit('adduser', '<?php echo $usermanager_user_company[0]['pc_users']['us_username'];?>');
});
$('#chatmsg').each(function () {
var list = $(this).find('li span').html();
socket.emit('USER_ONLINE',list);
});
socket.on('updatechat', function (username, data,rum) {
$('.'+rum).append('<li><b>'+username + ':</b> ' + data + '</li>');
});
socket.on('updaterooms', function (rooms, current_room) {
socket.emit('switchRoom', current_room);
});
function switchRoom (room) {
//alert(room);
active(room);
}
function active(room) {
//var message2 = $('.'+room).val();
//socket.emit('sendchat', room,message2);
$('.'+room).keydown(function (event) {
if(event.keyCode == 13) {
var message2=$(this).val();
// alert('Hi');
// tell server to execute 'sendchat' and send along one parameter
socket.emit('sendchat',room, message2);
//$('.messages li').last().focus();
}
});
}
var socket=io.connect('http://localhost:8585');
socket.on('connect',function(){
//调用服务器端函数“adduser”并发送一个参数(prompt的值)
emit('adduser','');
});
$('#chatmsg')。每个(函数(){
var list=$(this.find('li span').html();
socket.emit('USER_ONLINE',list);
});
socket.on('updatechat',函数(用户名、数据、rum){
$('.+rum).append(''+username+':'+data+' ');
});
socket.on('UpdateRoom',功能(房间、当前房间){
电源插座(‘开关室’、电流室);
});
功能配电室(室){
//警报(房间);
活动(房间);
}
活动功能(房间){
//var message2=$('..+room).val();
//socket.emit('sendchat',room,message2);
$('..+房间).keydown(功能(事件){
如果(event.keyCode==13){
var message2=$(this.val();
//警报(“Hi”);
//告诉服务器执行“sendchat”并发送一个参数
socket.emit('sendchat',room,message2);
//$('.messages li').last().focus();
}
});
}
Hi。在stackoverflow,我们帮助有特定问题的人。我担心仅仅发布代码并说它不起作用是不足以让人们轻易地帮助你的。请指定您遇到的问题。谢谢,我可以按组与多个用户聊天。但是,当不同的用户使用其帐户登录时,这些用户可以同时进行对话。不作为公共组,您可以让两个客户端加入一个唯一命名的聊天室,在该聊天室中,消息将仅在他们之间交换(理论上,他们可以选择邀请其他人),或者在服务器端的两个可以识别的缓存套接字之间交换消息。