简体   繁体   English

确定XMPP用户是否在线

[英]Determine if an XMPP user is online or not

I'm using the xmpppy library to write an XMPP client that can chat with users. 我正在使用xmpppy库来编写可以与用户聊天的XMPP客户端。 It has its own XMPP user account and needs to know if a given user is online. 它有自己的XMPP用户帐户,需要知道给定用户是否在线。 However, the documentation is a bit sparse on how to do this. 但是,如何执行此操作的文档有点稀疏。 What would you recommend? 你会推荐什么?

The only solution I've seen thus far is to start up a daemon before the XMPP server starts and monitor all presence messages that are sent out - then a user is said to be online if they've sent the "I'm online"-type message but not the corresponding "I'm logging off" message. 到目前为止,我看到的唯一解决方案是在XMPP服务器启动之前启动一个守护程序并监视发出的所有在线消息 - 如果用户发送了“我在线”,则称该用户在线-type消息但不是相应的“我正在注销”消息。 However, being new to XMPP in general, I would think there would be a nicer way to do this. 但是,作为XMPP的新手,我认为会有更好的方法来做到这一点。

The simple way is to support "subscribe" presence message -- this lets another user check if you're currently present (if they don't already know) by a "subscribe" attempt. 简单的方法是支持“订阅”状态消息 - 这允许另一个用户通过“订阅”尝试检查您当前是否存在(如果他们还不知道)。 Check this useful guide to get started, and the standard for many more important details (esp. on protecting your privacy, if needed, from subscribe requests from user you don't know). 查看这个有用的入门指南 ,以及更多重要细节的标准 (特别是如果需要,保护您的隐私来自您不知道的用户的订阅请求)。

There are basically three ways to connect to an XMPP server: as a client (which you've done), as a component, and as another server. 基本上有三种方法可以连接到XMPP服务器:作为客户端(您已经完成),作为组件,以及作为另一个服务器。 The server-to-server type (s2s) is just a federated connection, very much like how mail servers exchange email with each other. 服务器到服务器类型(s2s)只是一个联合连接,非常类似于邮件服务器如何相互交换电子邮件。

Alex described how clients keep track of presence. 亚历克斯描述了客户如何跟踪存在。 XMPP requires me to approve that you can receive my presence information and vice versa. XMPP要求我批准您可以接收我的状态信息,反之亦然。 For your bot this means for you to keep track of who's online the end users need to accept your presence requests. 对于您的机器人,这意味着您可以跟踪最终用户接受您的状态请求所需的在线用户。 It also means that you can respond to the user's presence requests and keep them informed about if your bot is up or not. 这也意味着您可以响应用户的状态请求,并随时了解您的机器人是否已启动。

The last way is as a trusted component, and only works if you're running the server. 最后一种方法是作为可信组件,只有在运行服务器时才有效。 ie if you're trying to do this on the jabber.org server, you're out of luck, because you're not running that server. 即如果你试图在jabber.org服务器上这样做,你运气不好,因为你没有运行该服务器。 The upsdie is you can have access to the internals of the XMPP server, like pulling lists of everyone who's online. upsdie是你可以访问XMPP服务器的内部,比如拉动每个在线人员的列表。 The downside is your component / bot implementation is going to be different for every server implementation. 缺点是您的组件/机器人实现对于每个服务器实现都会有所不同。

You might want to see the question "Jabber bot - how to get the availability of contacts?" 您可能希望看到问题“Jabber bot - 如何获得联系人的可用性?” . If I understood your question correctly, I think it's related to it. 如果我理解你的问题,我认为这与它有关。

Of course, as Alex pointed out, in any case you need to be subscribed to the users' presence – but you seem to have understood that. 当然,正如Alex指出的那样,无论如何你需要订阅用户的存在 - 但你似乎已经理解了这一点。

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

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