javascript XMPP 同一用户的多个会话问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9460488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:43:52  来源:igfitidea点击:

XMPP multiple sessions of the same user issue

javascriptxmppstrophepunjab

提问by vian

I've implemented a chrome extension which allow to use XMPP chat over BOSH connection with punjab server running on a remote server. It is implemented using javascript Strophe library. The issue I'm running into is when I have multiple sessions of the same user (e.g. two different browsers on the same machine) I can't receive and log to the second chat window the message I sent from first chat window. There needs to be some mechanism which allows that. Can I somehow receive messages I have sent to some other user? The issue can also be reproduced on 2 or more different machines so this needs to be solved too.

我已经实现了一个 chrome 扩展,它允许通过 BOSH 连接使用 XMPP 聊天与在远程服务器上运行的旁遮普服务器。它是使用 javascript Strophe 库实现的。我遇到的问题是,当我有同一个用户的多个会话(例如,同一台机器上有两个不同的浏览器)时,我无法接收我从第一个聊天窗口发送的消息并将其登录到第二个聊天窗口。需要有某种机制允许这样做。我可以以某种方式接收我发送给其他用户的消息吗?该问题也可以在 2 台或更多不同的机器上重现,因此这也需要解决。

Thank you.

谢谢你。

回答by ggozad

You need to understand how JIDs work, what priorities are and how to send messages.

您需要了解 JID 的工作原理、优先级是什么以及如何发送消息。

A JID is of the form: user@domain/resource

JID 的格式如下: user@domain/resource

The JIDs of logged in users have to be unique. Typically when you use a web client you assign a random resource to each session so as to not have clashes.

登录用户的 JID 必须是唯一的。通常,当您使用 Web 客户端时,您会为每个会话分配一个随机资源,以免发生冲突。

Now, when a user sends a message the toattribute of the <message>stanza specifies the recipient of the message. If the resource is part of the recipient then only that JID will receive the message. If the recipient is a bare JID (user@domain) then priorities come into play (see here):

现在,当用户发送消息时to,该<message>节的属性指定了消息的接收者。如果资源是收件人的一部分,则只有该 JID 会收到消息。如果接收者是一个裸 JID (user@domain),那么优先级就会发挥作用(见这里):

  1. The resource with the highest priority at any given time will be the one which receives incoming messages.
  2. If two or more resources have the same priority, all resources with said priority may receive incoming messages or depending on the server implementation one may receive depending to server-specific criteria.
  3. If all connected resources have a negative priority, incoming messages will be queued server-side until one of the resources resets priority to be positive.
  1. 在任何给定时间具有最高优先级的资源将是接收传入消息的资源。
  2. 如果两个或更多资源具有相同的优先级,则所有具有所述优先级的资源都可以接收传入消息,或者根据服务器实现,根据服务器特定的标准可能接收到的消息。
  3. 如果所有连接的资源都具有负优先级,则传入消息将在服务器端排队,直到其中一个资源将优先级重置为正优先级。

You can set the priority (an integer in [-128, 127])when you send your presence (see the rfcfor full spec) for example:

您可以设置优先级(一个整数[-128,127]),当你把你的存在(参见RFC全规格),例如:

<presence>
  <status>Learning XMPP</status>
  <priority>1</priority>
</presence>

回答by Zash

If you want to have the entire conversation, including messages you send fromyour client(s) to show up on another session, then Carbonsis the feature you're looking for. I've implemented this in a pluginfor Prosody.

如果您想拥有整个对话,包括您客户发送的消息以显示在另一个会话中,那么Carbons就是您正在寻找的功能。我在一个插件来实现这个韵律

The required client part shouldn't be too hard to write, here's it done in the Verse library.

所需的客户端部分不应该太难写,这是在 Verse 库中完成的

回答by David

Make a long story short!

长话短说!

Use:

利用:

mXmppConnection.login (USERNAME, PASSWORD, StationName/NickName);

And not:

并不是:

mXmppConnection.login (USERNAME, PASSWORD);

The last parameter called resource, and represents your station that you login from.

最后一个参数称为资源,代表您登录的站点。

That way you can login with the same username, but still from 2 devices.

这样您就可以使用相同的用户名登录,但仍然可以从 2 个设备登录。

回答by Debashis Chowdhury

If you enable Carbons: XEP-0280: Message Carbonswhile you detect multiple login, XMPP server will send a carbon message to your other sessions which are logged in on different devices

如果您启用 Carbons: XEP-0280: Message Carbons当您检测到多次登录时,XMPP 服务器将向您在不同设备上登录的其他会话发送一个 carbon 消息

<enable xmlns='urn:xmpp:carbons:2'/>

Remember to enable it for all sessions. So, both sessions will get the sending and receiving messages. For the case of receiving message, if you are carbon enabled, in presence will not effect.

请记住为所有会话启用它。因此,两个会话都将获得发送和接收消息。对于接收消息的情况,如果您启用了 carbon,则在场不会生效。

Again, if you want a message not to be carbon copy, add <private/>, <no-copy/>inside stanza

同样,如果您希望消息不是抄送<private/>,请<no-copy/>在节中添加 ,

<private xmlns='urn:xmpp:carbons:2'/>
<no-copy xmlns='urn:xmpp:hints'/>

If the carbons module is not activated in your XMPP server, you need to activate it.

如果您的 XMPP 服务器中的 carbons 模块未激活,则需要将其激活。