In the Java open Source implementation OpenFire of XMPP, increase the human function near lbs

Source: Internet
Author: User
Tags cos pow sin

1. XMPP protocol and OpenFire

The XMPP protocol is a standard protocol in the IM domain, with reference to http://xmpp.org and rfc6120,rfc6121,rfc6122 and other related documents. http://xmpp.org/xmpp-protocols/

XMPP protocol implementation, there are many open source, server-side reference http://xmpp.org/xmpp-software/servers/client can be referenced http://xmpp.org/xmpp-software/ Clients/library, etc. can be referenced http://xmpp.org/xmpp-software/libraries/

The Java implementation of the XMPP protocol is openfire popular, perhaps depending on the low threshold of installation and the classic implementation of the underlying Mina framework.

Http://igniterealtime.org/projects/openfire

2. People near lbs

On mobile devices, almost most of the IM class applications have the "nearby people" function, the principle is very simple, each user to see their own nearby users, you must upload their geographical location (GPS, base station location) to share;

The server side pushes other user information within a certain radius to users by computing two points of distance on the Earth.

Specific algorithm implementation, we refer to common practices, such as from Zhihu on the share of peers:

Indexing with latitude and longitude,

  1. First coarse calculation, such as the longitude and latitude of more than one of the total removal, where latitude>y-1 and latitude<y+1 and longitude>x-1 and longitude <x+1 and ...; X, Y is the latitude and longitude of the current user.

  2. In the case of a small scope estimate, use a formula like this order by ABS (Longitude-x) +abs (latitude-y) limit 100;

  3. The final display is then calculated using a formula like this: (2 * 6378.137* ASIN (SQRT (POW (SIN (PI () * (Y-lat)/360), 2) +cos (pi () *x/180) * COS (LAT * PI ()/180) *pow ( SIN (PI () * (X-LNG)/360), 2))).

The first two items are computed on the database side, and the latter is calculated on the application server side.

3. XMPP protocol Extension

If you want to increase the LBS functionality on the XMPP protocol, then we need to extend XMPP to add new request and response messages.

There are two ways to expand XMPP, one is the official extension, see XEP0080 http://xmpp.org/extensions/xep-0080.html, is to add lbs information in the message;

Example 1. Entity publishes location

<iq type= ' Set ' from= ' [email protected]/pda ' id= ' publish1 ' >
<pubsub xmlns= ' http://jabber.org/protocol/pubsub ' >
<publish node= ' Http://jabber.org/protocol/geoloc ' >
<item>
<geoloc xmlns= ' http://jabber.org/protocol/geoloc ' xml:lang= ' en ' >
<accuracy>20</accuracy>
<country>Italy</country>
<lat>45.44</lat>
<locality>Venice</locality>
<lon>12.33</lon>
</geoloc>
</item>
</publish>
</pubsub>
</iq>

Example 2. Subscriber receives event with payload

<message from= ' [email protected] '
to= ' [email protected] ' >
<event xmlns= ' http://jabber.org/protocol/pubsub#event ' >
<items node= ' Http://jabber.org/protocol/geoloc ' >
<item id= ' d81a52b8-0f9c-11dc-9bc8-001143d5d5db ' >
<geoloc xmlns= ' http://jabber.org/protocol/geoloc ' xml:lang= ' en ' >
<accuracy>20</accuracy>
<country>Italy</country>
<lat>45.44</lat>
<locality>Venice</locality>
<lon>12.33</lon>
</geoloc>
</item>
</items>
</event>
</message>

Another way to do this is by adding a custom IQ directive, such as our design:

REQUEST

<iq id= "C919" type= "Get" from= "[email protected]/tcl-s960" ><query xmlns= "Com.nodexy.im.openfire.location" > <item user= "Chris" lon= "22.323009" lat= "29.098763"/></query>

Attention:

The default IQ does not set the To property, which means it is sent to OpenFire server, or to=im.nodexy.com;

If User A wants to send its own geo-location share to friend User B, you will need to explicitly set [email protected]; The server will only forward this IQ message and will not do any other processing.

RESPONSE

<iq id= "C919" type= "result" from= "[email protected]/tcl-s960" ><query xmlns= " Com.nodexy.im.openfire.location "> <item user=" chris1 "lon=" 22.323009 "lat=" 29.098763 "sex=" 0 "online=" 30min "/&    Gt <item user= "Chris2" lon= "22.323009" lat= "29.098763" sex= "0" online= "30min"/> <item user= "Chris3" lon= "22.3230 "Lat=" 29.098763 "sex=" 0 "online=" 30min "/> ... ...</query>

The pros and cons of the above two approaches:

    1. XEP 0080: The official expansion agreement, more general, also more in line with LBS is a special message concept, but the customization is not strong, can not increase their own many business logic, especially the "nearby people" function is not included;

    2. Increased IQ Instructions: more flexible, on-demand use, support "nearby people" even "nearby businesses", of course, the disadvantage is not universal, is a private protocol, in the form of private plug-in implementation.

In this paper we mainly adopt the second kind.

4. OpenFire plug-in implementation

To implement the LBS function in OpenFire, we can implement the above extension protocol in the way of developing new plugins.

About OpenFire plug-in development can refer to http://www.igniterealtime.org/builds/openfire/docs/latest/documentation/plugin-dev-guide.html

5. Code sharing

I am here to share a basic version of the openfirelbs plugin Https://github.com/node/openfireLBS

In the Java open Source implementation OpenFire of XMPP, increase the human function near lbs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.