XMPP multiple sessions of the same user issue

我的梦境 提交于 2019-12-03 02:25:24

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

A JID is of the form: 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.

Now, when a user sends a message the to attribute 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):

  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.

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

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

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

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

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.

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

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!