سؤال

My Real Time Chat App is based on Sails and SocketIO.It shows all users along with available online users .When a User logged in,a UserConnected socket is emitted and When a User is disconnected,a UserDisconnected socket is emitted , My Server side socketio code in config/sockets.js is

onConnect: function(session, socket) {
  if (session.passport) {
    if (session.passport.user && socket.id) {
      sails.io.sockets.emit('UserConnected', {userID : session.passport.user});                       }
    }
  }
  //// By default: do nothing
  //// This is a good place to broadcast a disconnect message, or any other custom socket.io logic
},

onDisconnect: function(session, socket) {
  if (session.passport) {
    if (session.passport.user && socket.id) {
     sails.io.sockets.emit('UserDisconnected', {userID : session.passport.user});
    }
  }
  //// By default: do nothing
  //// This is a good place to broadcast a disconnect message, or any other custom socket.io logic
}

The Problem is that when a User refreshes the page ,it shows connected and disconnected to another User and one major problem is that if one User logged in two different browsers and if any one browser is closed is shows User disconnected to other Users but the User is actually available in another browser.
How would I modify/code So that these problems will be fixed?
Thanks in Advance

هل كانت مفيدة؟

المحلول

@InternalFX posted a good method; an alternative that you can use on Sails v0.10.x would be:

  onConnect: function(session, socket) {

    // If this is a logged in user, subscribe the socket to a 
    // custom "loggedInCount" room
    if (session.user) {
      var roomName = 'loggedIn'+session.user.id;
      sails.sockets.join(socket, roomName);
      // If this is the first subscriber, the user is just coming online, 
      // so notify any subscribers about the state change.
      if (sails.sockets.subscribers(roomName).length == 1) {
        User.message(session.user.id, {state: 'online'}, socket);
      }
    }

  },

  onDisconnect: function(session, socket) {

    if (session.user) {
      var roomName = 'loggedIn'+session.user.id;
      sails.sockets.leave(socket, roomName);
      // If this was the last subscriber, the user is going offline, 
      // so notify any subscribers about the state change.
      if (sails.sockets.subscribers(roomName).length == 0) {
        User.message(session.user.id, {state: 'offline'}, socket);
      }
    }

  },

This uses Sails' built-in pubsub architecture to notify connected sockets whenever a particular user comes online or goes offline. It requires sockets to subscribe to the user instances they want to know about (using something like socket.get('/user') on the client), which is a good practice to get into. This way you can be notified only about the users you care about (e.g. if you had a "friends list").

نصائح أخرى

Here is the method I am using for a similar purpose.

var online_users = {};

module.exports.sockets = {

  onConnect: function(session, socket) { 
    if (_.isString(session.user_id)) {
      if (_.has(online_users, session.user_id)) {
        online_users[session.user_id] += 1;
      } else {
        online_users[session.user_id] = 1;
      }
    }
    sails.io.sockets.emit('crm/online_users', online_users);
  },
  onDisconnect: function(session, socket) {
    if (_.isString(session.user_id)) {
      if (_.has(online_users, session.user_id)) {
        online_users[session.user_id] -= 1;
      } else {
        online_users[session.user_id] = 0;
      }
    }
    sails.io.sockets.emit('crm/online_users', online_users);
  },
}

It keeps track of how many browsers are logged in as a specific user. So you could use the following.

if (online_users[session.user_id] > 0) { console.log('USER IS LOGGED IN'); }`
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top