简体   繁体   中英

Socket.io Won't Emit messages to rooms on discconect

Basically I'm trying to create a quick chat room using Node.JS and angular.

I've Used other socket events and they worked I don't quite get why they don't want to work now. The really weird thing is that The login works fine and quite well, but when It comes to the logout, or even using my disconnect method, Nothing happens on the server side, with the disconnect function, the console.log works fine. But It won't emit.

On the angular side all my console.log s are running yet no events hit the server when logging out or discconecnting.

Inside of Node I have Three Events

 socket.on('chat:userLogin', function(user) {
    socket.join('online-users');
    chatRoom[user.id] = user
    console.log('User Has logged in, chat room contains: ', chatRoom);
    io.sockets.in('online-users').emit('chat:usersOnline', chatRoom)
  })

  socket.on('chat:userLogout', function(userId) {
    console.log('User Id ' + userId + 'is logging out');
    delete chatRoom[userId]
    console.log('User Has logged out, chat room contains: ', chatRoom);
    io.sockets.in('online-users').emit('chat:userLeave', chatRoom);
    socket.leave('online-users');
  })

  socket.on('disconnect', function() {
    var userRoom = rooms[socket.roomId]

    if (chatRoom[socket.userId])
      delete chatRoom[socket.userId]

    io.sockets.in('online-users').emit('chat:userLeave', socket.userId);
    console.log('Emitting that users have left to: ', io.sockets.in('online-users'));
    socket.leave('online-users');
  }

And in angular I have a Two functions that emit these two events

  this.logout = function(userId) {
      console.log('Logging user out: ', userId);
      Socket.emit('chat:userLogout', userId);
      console.log('Logged out user?');
      delete this.userStore[userId]
    }

    this.login = function(userId) {
      console.log('Getting online user ', userId);
      var self = this;
      User.get({id: userId}, function(user) {
        self.userStore[userId] = user;
        Socket.emit('chat:userLogin', user);
      });
    }

It may fix your issue but you should be using socket.broadcast.to('roomname') unless you want to send the message to all sockets in the room (including the one that sent the message). So your code would look something like

socket.on('chat:userLogin', function(user) {
   socket.join('online-users');
   chatRoom[user.id] = user
   console.log('User Has logged in, chat room contains: ', chatRoom);
   // emit to everyone else except the sender
   socket.broadcast.to('online-users').emit('chat:usersOnline', chatRoom);
})

socket.on('chat:userLogout', function(userId) {
   console.log('User Id ' + userId + 'is logging out');
   delete chatRoom[userId]
   socket.leave('online-users');
   // leaving the chat room here means we can emit to all people in the room
   io.sockets.in('online-users').emit('chat:userLeave', chatRoom);
   console.log('User Has logged out, chat room contains: ', chatRoom);

})

socket.on('disconnect', function() {
   var userRoom = rooms[socket.roomId]

   if (chatRoom[socket.userId])
      delete chatRoom[socket.userId]

   socket.leave('online-users');
   io.sockets.in('online-users').emit('chat:userLeave', socket.userId);

   console.log('Emitting that users have left to: ', io.sockets.in('online-users'));
}

Check out this question for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM