Hello I am currently working on some kind of "Chat"-system.
Server: NodeJs with express - https Communication: Socket.io@1.0
client:
var socket = io.connect();
socket.emit('HELLO', {user:'someuserid'});
socket.on('WELCOME', function(data){
socket.emit('REGISTER', {});
});
socket.on('SIGNED', function(data){
console.log(data.user);
socket.emit('GO', {});
});
socket.on('message', function(data){
console.log(data.somedata);
});
server:
io.sockets.on('connection', function(socket){
socket.on('HELLO', function(data){
socket.user = data.user;
socket.emit('WELCOME', socket.user);
});
socket.on('REGISTER', function(data){
console.log('room: '+socket.user);
socket.join(socket.user);
socket.emit('SIGNED', socket.user);
console.log('SIGNED IN: '+socket.user);
});
socket.on('GO', function(data){
//some logic happens where a list of users gets loaded
//which is identical to the socket.user and its room
//... loop
io.in(partners[i].user).emit('message', { somedata: 'asd' });
}
socket.on('disconnect' ..... blaaa blaa)
So, basicly what I tried to do here is create a workaround to sending a message to a specific user by sending a message to a specific room.
this:
io.in(partners[i].user).emit('message', { somedata: 'asd' });
and this:
partners[i].socket.emit('message', { somedata: 'asd' });
result in the same:
room: 540246a3e4b0a64a28e1ec59
SIGNED IN: 540246a3e4b0a64a28e1ec59
room: 540504ba0b526b274398480e
SIGNED IN: 540504ba0b526b274398480e
to: 540246a3e4b0a64a28e1ec59
disconnected:540246a3e4b0a64a28e1ec59
the user registers, gets connected and wants to emit a message to specific chatpartners in the array partners[i].
once the emit is fired the user, the message is supposed to be emitted to disconnects...
what am I doing wrong?
(the script is obviously not complete.. i pasted the most important parts)
Thanks for your help.
I think I have found the solution:
sometimes using google is more effort than debugging by yourself.
After scrolling through the debug log in firefox I found out that this problem actually had to do with the code inside my socket 'message' handler. A snippet that might help others to find errors with their sockets:
socket.on('error', function (err) {
if (err.description) throw err.description;
else throw err; // Or whatever you want to do
});
I think this is an issue in socket.io although it was my wrong code - the socket.io errorhandler should have passed that through.
Have fun