简体   繁体   English

Laravel Echo 和 Laravel Echo 服务器和套接字:区分服务器端的加入和离开事件

[英]Laravel Echo and Laravel Echo server and socket: Distinguish join and leave events in server side

I am building a web service for a mobile application.我正在为移动应用程序构建 Web 服务。 I need to know which mobile users are online.我需要知道哪些移动用户在线。 I implemented this by Laravel echo- laravel echo server-socket io and redis.我通过 Laravel echo-laravel echo server-socket io 和 redis 实现了这个。 Every thing works and by presence channel the client listens to the events.一切正常,客户端通过在线渠道监听事件。

The problem is, the leave and join events are on client side, while when a user leaves the channel, I need to remove it from a list stored in Redis, In fact I need to know that which of the join or leave occurs in server side not client side in order to be able to update the online users in Redis.问题是,离开和加入事件在客户端,而当用户离开频道时,我需要从存储在Redis的列表中删除它,实际上我需要知道哪个加入或离开发生在服务器端不是客户端,以便能够更新 Redis 中的在线用户。

How can I do this?我怎样才能做到这一点?

My solutions我的解决方案

Solution 1解决方案1

If there is any implementation of here method in server side which returns the online users, it may work, in fact I can compare the my list with here to get the latest update.如果服务器端有返回在线用户的here方法的任何实现,它可能会起作用,实际上我可以将我的列表与here进行比较以获取最新更新。

Solution 2解决方案2

When an event happens for a user, if that user already exists in my list in the Redis, clearly it is a leave event, because users can be online exactly once at any moment, and if the user doesn't exist in the list, so it must be a join, because no one can join if it is already on the list.当用户发生事件时,如果该用户已存在于我的 Redis 列表中,则显然是离开事件,因为用户在任何时候都只能在线一次,如果该用户不存在于列表中,所以它必须是一个连接,因为如果它已经在列表中,没有人可以加入。

Thanks in advance.提前致谢。

You should be using Presence Channels for this functionality.您应该使用Presence Channels来实现此功能。

Watch this Laracast Episode 8 video, you can adapt the examples there with your offline user list.观看此 Laracast 第 8 集视频,您可以使用离线用户列表调整那里的示例。

I suggest pushing the user.id of an online user into an participants array list using channel here , joining , leaving hooks as the video demonstrates.我建议使用 channel here将在线用户的user.id推送到participants数组列表中, joining ,如视频所示leaving钩子。

When outputting your full list of users , you could check if they are online using includes .在输出完整的users列表时,您可以使用包含检查他们是否在线。


const participants = [4, 34, 57, 70]

for (user of users) {

  if( participants.includes(user.id) ) {
   // The user is online, Set an online style or something
  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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