Android XMPP Server, BOSH (http-binding) and Web client building

Source: Internet
Author: User

Objective: To build an XMPP server that communicates with your XMPP server using JavaScript on the Web page, anonymously, and communicates with any XMPP (Jabber) account. (Gtalk is still having problems)

XMPP server may not be required (see below, I did not try)

Environment and configuration:

XMPP server: ejabberd documentation
Http-binding: Built with Ejabberd, 5280 ports.
Javascript Client:strophe Documentation

Installing Ejabberd
yuminstallejabberd#apt-get install Ejabberd

Edit Config file:/etc/ejabberd/ejabberd.cfg, this is an era lang format configuration file, the line comment symbol is%. Please refer to the Ejabberd documentation.

Here is the default profile in which I have modified the section:

%%Debug{loglevel,5}. {hosts, ["sagan.me"]}. {host_config,"sagan.me", [{auth_method, [Anonymous,Internal]},{anonymous_protocol, Sasl_anon}]}. {Listen, [{5222, Ejabberd_c2s, [{certfile,"/path/to/ssl/cert.pem"},                        %%Starttls, starttls_required, {access, C2s}, {Shap Er, c2s_shaper}, {max_stanza_size,65536}                       ]},  {5269, ejabberd_s2s_in, [{shaper, S2s_shaper}, {max_stanza_size,131072}                          ]},  {{5280,"127.0.0.1"}, Ejabberd_http, [{request_handlers, [{["Http-bind"], mod_http_bind}]}, Captcha]}]}. {S2S_USE_STARTTLS,true}. {s2s_certfile,"/path/to/ssl/cert.pem"}. {S2s_default_policy, allow}. {Auth_method, [Internal, Anonymous]}.

In the above configuration, the assertion listener 127.0.0.1 (local IP address) 5280 port is the http-binding (BOSH) service address, the path is "Http-bind", that is, the actual URI of the service is "Http://127.0.0.1:5280/http-bind". You then need to forward the "/http-bind" access on the 80 or 443 port to "Http://127.0.0.1:5280/http-bind" in the Web server configuration with Mod_proxy or mod_rewrite, because of browser-origin limitations, The Web page on YOURDOMAIN.COM:80 cannot submit AJAX requests directly to yourdomain.tld:5280. (So in the above configuration, the Ejabberd Http-bind listening port is set to 127.0.0.1:5280, which is not directly accessible from the outside)

Add domain name DNS SRV records

This step is necessary, otherwise the XMPP server being built is basically unable to communicate with most other servers or clients. (insert: Google apps talk based on the XMPP platform, if you do not set the domain name SRV record, only enough gtalk login (unable to use other XMPP client), and can only communicate with gmail.com or other Google Apps domain name account)

_xmpp-client._tcp.sagan.me. 86400 in SRV 0 5222 sagan.me.
_xmpp-server._tcp.sagan.me. 86400 in SRV 0 5269 sagan.me.

5269 and 5222 are the standard ports that XMPP registers with ICANN.

Modify the Web server configuration

My lighttpd modproxy configuration:

Proxy.server = (        "/http-bind" = ("host                  "127.0.0.1",                "port"  5280         )))

You should also use the Web server to forward requests directly to an externally exposed Jabber (XMPP) server Http-bind address, which I did not attempt. (There is basically no publicly available XMPP server for http-bind)

Using a JavaScript client

Anonymous login (anoymous mechanism) is turned on in the Ejabberd configuration above in order to anonymously access the service in the Web page and send a message to any XMPP account.

Download Strophe JS Library and upload to your domain name directory (this library has only one file strophe.js), the following test example is modified from Strophe examples directory echobot.html

<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 strict//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-STRICT.DTD">"http://www.w3.org/1999/xhtml">'Text/javascript'src='Http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js'></script><script type='Text/javascript'src='.. /strophe.js'></script><script type='Text/javascript'src='Echobot.js'></script>'Login'style='Text-align:center'><form name='cred'><label for='Jid'>jid:</label><input type='text'Id='Jid'Value="sagan.me"/><label for='Pass'>password:</label><input type='Password'Id='Pass'/><input type='Button'Id='Connect'Value='Connect'/&GT;&LT;/FORM&GT;&LT;/DIV&GT;&LT;HR/><div id='Log'></div></body>

See Echobot.js

Varbosh_service ='/xmpp-httpbind'; Varconnection=NULL; Functionlog (msg) {$ ('#log'). Append ('<div></div>'). Append (document.createTextNode (msg));} Functiononconnect (status) {if(status==Strophe.Status.CONNECTING) {Log ('strophe is connecting.');} ElseIf (Status==Strophe.Status.CONNFAIL) {Log ('Strophe failed to connect.');$('#connect').Get(0). value='Connect';} ElseIf (Status==Strophe.Status.DISCONNECTING) {Log ('strophe is disconnecting.');} ElseIf (Status==Strophe.Status.DISCONNECTED) {Log ('strophe is disconnected.');$('#connect').Get(0). value='Connect';} ElseIf (Status==Strophe.Status.CONNECTED) {Log ('Strophe is connected.'); log ('echobot:send a message to'+ connection.jid+'To talk to me.'); Connection.addhandler (OnMessage,NULL,'message',NULL,NULL,NULL); Connection.send ($pres (). tree ()); Varreply= $msg ({to:"[email protected]", from: Connection.jid, type:'Chat'}). C ("Body"). T ("Test Chat Message"); Connection.send (Reply.tree ());}} Functiononmessage (msg) {Varto= Msg.getattribute (' to'); Varfrom= Msg.getattribute (' from'); VarType= Msg.getattribute ('type'); Varelems= Msg.getelementsbytagname ('Body');if(Type = ="Chat"&&elems.length>0) {Varbody= elems[0];log ('Echobot:i got a message from'+ from+': '+Strophe.gettext (body)); Varreply= $msg ({to: from, from: To, type:'Chat'}). Cnode (Strophe.copyelement (body)); Connection.send (Reply.tree ()); Log ('Echobot:i sent'+ from+': '+Strophe.gettext (body));}//We must return true to keep the handler alive.//returning false would remove it after it finishes.Returntrue;} $ (document). Ready (function () {connection=newstrophe.connection (bosh_service);//uncomment the following lines to spy in the wire traffic.//connection.rawinput = function (data) {log (' RECV: ' + data);};//connection.rawoutput = function (data) {log (' SEND: ' + data);};//Uncomment the following line to see all the debug output.//Strophe.log = function (level, msg) {log (' log: ' + msg);};$('#connect'). Bind ('Click', function () {Varbutton= $('#connect').Get(0);if(button.value=='Connect') {Button.value='Disconnect'; Connection.connect ($ ('#jid').Get(0). value,$ ('#pass').Get(0). Value,onconnect);}Else{Button.value='Connect'; connection.disconnect ();});});

Modify [email protected] to a test jabber account. Then open echobot.html with the browser, click the Connect button, Strophe will be anonymous login to the newly established Ejabber server (sagan.me), and "[email protected]" This account to send a "Test Message ".

Another: Test anonymous login to [email protected] Send a message failed, log shows Gtalk server return information is 503 error, service-unavailable, but if you log in and add Gtalk as a friend is OK. Gtalk prohibit anonymous users from sending messages to them? I'm still checking the information.

I'm going to use JavaScript to write a simple XMPP Web anonymous client that allows visitors to communicate directly with Gtalk and Facebook chat, among other features

Http://www.ibm.com/developerworks/cn/xml/tutorials/x-realtimeXMPPtut/index.html

Android XMPP Server, BOSH (http-binding) and Web client building

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.