我使用node.js和socket.io开发实时应用程序。我的问题是我使用PHP登录用户。在连接上,用户将信息发送到服务器。但是,如果用户打开具有相同登录数据的第二个选项卡。新标签页不会从服务器获取通知。只有旧套接字。
client.js
:
var socket = io('http://192.168.178.165:3000');
socket.on('auth',function () {
socket.emit('auth resp',{'name':user_name,'id':user_id});
});
server.js
:
var count = 0;
var users = {};
var io = require('socket.io')(3000);
io.sockets.on('connection', function(client) {
client.emit('auth');
client.on('auth resp',function (data) {
if(data.id in users) {
//User has second socket
} else {
client.u_name = data.name;
client.u_id = data.id;
users[client.u_id] = client;
count++;
}
});
client.on('disconnect', function(){
count--;
delete users[client.u_id];
});
client.on('join',function(room){
client.join(room);
});
client.on('leave',function(room){
client.leave(room);
});
});