简体   繁体   English

在线用户和服务器指标

[英]Online users and server metrics

I wanted to have an online users counter in my website and at first I thought about including a websocket snippet in every page but this seemed a little overkill to me since I don't really need a constant ping-pong every half a second but rather a ping once every five seconds (for example). 我想在我的网站上建立一个在线用户计数器,起初我想在每个页面中都包含一个websocket片段,但这对我来说似乎有点过头了,因为我真的不需要每隔半秒钟就不断进行乒乓球运动,而是每五秒钟执行一次ping(例如)。 For performance reasons, I guess the best approach is to have the clients send the ping to the server, while the last one only receives it without sending anything back. 出于性能方面的原因,我认为最好的方法是让客户端将ping发送到服务器,而最后一个仅接收ping而不会发送任何东西。 I would like to take metrics too as in bandwidth consumption or how long a user is online. 我也想衡量带宽消耗或用户在线时间。

How would you do it? 你会怎么做? Websocket or by posting a JSON somewhere every X seconds? Websocket还是每X秒在某个地方发布JSON? Any out of the box solution you can think of? 您能想到的任何现成的解决方案吗? Thanks 谢谢

I think the most accurate of results would be achieved using a web socket. 我认为使用网络套接字可以实现最准确的结果。 Here is a very high level implementation, you can fill the meat in. 这是一个非常高级的实现,您可以填写内容。

  1. Sockets have events for connection, disconnection, and errors so you only need to register a user statistic on socket connection event. 套接字具有用于连接,断开连接和错误的事件,因此您只需要注册关于套接字连接事件的用户统计信息。

  2. Deregister the user statistic on socket disconnection event. 注销有关套接字断开事件的用户统计信息。

  3. Socket connection event can send information such as page accessed by user and any other information you may wish to send. 套接字连接事件可以发送信息,例如用户访问的页面以及您可能希望发送的任何其他信息。

  4. There does not need to be any ping/pong code, socket events will take care of everything themselves. 不需要任何ping / pong代码,套接字事件将自己处理所有事情。

  5. You can offload this connection from the main server and have a separate server for collection of user statistics. 您可以从主服务器上卸载此连接,并拥有一个用于收集用户统计信息的单独服务器。

If you have maintained your code well, there will not be a need to add socket snippet to every page. 如果您已经很好地维护了代码,则无需在每个页面上添加套接字代码段。 You will just need to add it in a template, say for example. 例如,您只需要将其添加到模板中即可。

The other cheaper but not very accurate solution could be to check the session files, say every 15 minutes. 另一个较便宜但不太准确的解决方案是每15分钟检查一次会话文件。 All session files that were not accessed in the last 15 minutes are disconnected users so subtract them from the total number of session files and you will get the users who have been online in the last 15 minutes. 最近15分钟内未访问的所有会话文件都是断开连接的用户,因此从会话文件总数中减去它们,您将获得最近15分钟内处于在线状态的用户。 I am not sure how you would collect detailed statistics using this approach though. 我不确定您将如何使用这种方法收集详细的统计信息。

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

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