如何使用socketio实时显示在线/离线用户使用Sails? [英] How to use socketio for showing Online/Offline User in Real Time Using Sails?

查看:576
本文介绍了如何使用socketio实时显示在线/离线用户使用Sails?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实时聊天应用基于 Sails SocketIO 。它显示所有用户以及可用的在线用户。当用户登录时,会发出UserConnected套接字,当用户断开连接时,会发出UserDisconnected套接字,
config / sockets.js中的服务器端socketio代码是

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发布了一个不错的方法;您可以在Sails v0.10.x上使用的替代方法是:

@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);
      }
    }

  },

这使用Sails的内置pubsub架构,在特定用户上线或下线时通知连接的套接字。它需要套接字来订阅他们想要了解的用户实例(在客户端上使用类似 socket.get('/ user')的东西),这是一个很好的做法进入。这样,您只会收到有关您关注的用户的通知(例如,如果您有朋友列表)。

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").

这篇关于如何使用socketio实时显示在线/离线用户使用Sails?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆