Simple implementation of webim

Source: Internet
Author: User
There are many webim methods to achieve implementation principles. The most critical issue is to maintain the connection with the server. How to Ensure session immediacy and connectivity. Poll, long poll, and comet are commonly used. Among them, poll has the worst effect and generally has a latency of 1 to 2 seconds. The long poll and comet technologies are relatively new. Due to the stateless HTTP, this persistent connection method needs to be maintained and generally requires additional servers. Code Provide support. For example, Gtalk uses the long poll method. The server usually uses asynchronous Io to support high concurrency. This article uses the xep standard extension specification, XEP-0124 provides the method to achieve webim. The idea is to use a javascript xep-0124 implementation, directly connect to the XMPP server side. At present, the implementation of xep-0124 is supported in most of the XMPP server implementation, this article uses openfire 3.6.4 as XMPP server. The xep 0124 Specification defines a complete and secure protocol. For more information, see the documentation. In this paper, the implementation of JavaScript terminal XEP-0124 is strophe JS implementation. In addition, because of the cross-origin problem of browser JavaScript, we need to make webim call the Javascript port 80 with the same subdomain. Therefore, Apache or nginx is used on port 80, forward/HTTP-bind requests to the HTTP-binding port of the XMPP server. The local machine uses nginx. XMPP server to use port 7070. Structure: Web UI page.html | strophe. JS Ajax request/HTTP-bind/|> nginx/Apache port: 80/HTTP-bind/|> XMPP Server HTTP-bind port (of: 7070) /HTTP-bind/installation preparation download openfire 3.6.4 and install it. Http://www.igniterealtime.org/downloads/index.jsp download strophe JavaScript library http://code.stanziq.com/cgit/strophe/strophejs/snapshot/strophejs-master.tar.gz download nginx or Apache and install the configuration. Only one proxy forwarding server for port 80. To install and configure openfire, you need to open the http-binding of openfire. Specifically, you can log on to the background management interface. Server-> server settings-> HTTP binding: Check this item. Enabled-Clients can connect to this server using HTTP binding. BelowScript Syntax: Enabled-Allows bosh clients with limited access to connect to the server Use the default port.

In nginx configuration, open the nginx configuration file nginx. conf and add the following lines:
# Gzip on; # Add Upstream bk. openfire { Server 127.0.0.1: 7070; } Location/{root HTML; index index.html index.htm;} is added later: Location/HTTP-bind { Proxy_pass http://bk.openfire; Proxy_buffering off; Proxy_redirect off; Proxy_read_timeout 120; Proxy_connect_timeout 120; }

Black is the original configuration,Red indicates the row to be added.. Apache configuration

Add the following lines to httpd. conf:

 

 

Loadmodule proxy_module modules/mod_proxy.so
Loadmodule proxy_http_module modules/mod_proxy_http.so

Proxyrequests off
Proxypass/HTTP-bind http: // 127.0.0.1: 7070/HTTP-bind/
Proxypassreverse/HTTP-bind http: // 127.0.0.1: 7070/HTTP-bind/

 

Note: Apache must have a compilation proxy module. Write webim and configure the server above. Next we will develop our simple version of pure web im. decompress strophejs-master.tar.gz and you can get b64.js and md5.js sha1.js strophe. JS. This is the JS base class library we will use. This will be referenced on our page. It contains an examples directory. You can also learn the examples to familiarize yourself with the strophe syntax. In this example, we can modify it from the echobot.html example. The specific UI layout is omitted. The basic HTML elements are added with several styles. You can see the code clearly. Strophe is an excellent xep-0124 class library that can be easily integrated. The following code creates a connection in strophe in echobot. JS. Code 1: Use strophe to establish a server connection

Function onconnect (Status) {If (status = strophe. status. connecting) {log ('strophe is connecting. ');} else if (status = strophe. status. connfail) {log ('strophe failed to connect. '); $ (' # connect '). get (0 ). value = 'connect ';} else if (status = strophe. status. disconnecting) {log ('strophe is disconnecting. ');} else if (status = strophe. status. disconnected) {log ('strophe is disconnected. '); $ (' # connect '). get (0 ). value = 'connect ';} else if (status = strophe. status. connected) {log ('strophe is connected. ');Connection. addhandler (onmessage, null, 'message', null, null );Connection. Send ($ pres (). Tree ());}}
Connection = new strophe. Connection (bosh_service ); Connection. rawinput = rawinput; connection. rawoutput = rawoutput; // strophe. log = function (Level, MSG) {log ('Log: '+ MSG) ;};$ (' # connect '). BIND ('click', function () {var button = $ ('# connect '). get (0); If (button. value = 'connect ') {button. value = 'disconnect'; fromid = $ ('# jid '). val (); toid = $ ('# tojid '). val (); log (fromid); log (toid ); Connection. Connect ($ ('# jid'). Get (0). value, $ ('# Pass'). Get (0). value, Onconnect ); } Else {button. value = 'connect '; Connection. Disconnect (); }});
The above Code Description: Bosh_service: the URL of the HTTP binding of the XMPP server. Note: openfire is http: // 127.0.0.1: 7070/HTTP-bind/. We need to use Apache or nginx for proxy forwarding. Therefore, the configuration here is "/HTTP-bind /". Use the connection. Connect Method to connect to the server, and input the username, password, and the onconnect callback function. Connection. addhandler (onmessage, null, 'message', null, null ); This is a callback function for processing messages after the connection is successful. When a message is received, the onmessage function is called. Code 2: onmessage () function

Function onmessage (MSG ){ To = msg. getattribute ('from '); VaR from = msg. getattribute ('from '); VaR type = msg. getattribute ('type '); VaR elems = msg. getelementsbytagname ('body '); If (type = "chat" & elems. length> 0 ){ VaR body = elems [0]; Appendtohis (new date (). tolocaletimestring () + ''+ from + 'say: '+ Strophe. gettext (Body) ); } // We must return true to keep the handler alive. // Returning false wocould remove it after it finishes. Return true; }

Here, MSG is the received message. In the row strophe. gettext (body), the received IM message is returned. MSG has some other content. If you care about it, you can proceed with it.Strophe sends messages

MSG = $ ('# MSG'). Val (); toid = $ ('# tojid'). Val ();VaR reply = $ MSG ({to: toid, from: fromid, type: 'chat'}). cNode (strophe. xmlelement ('body', '', MSG ));Connection. Send (reply. Tree ());
It is also very simple. First assemble a message and then call connection. Send () to send the message. Strophe closes the connection
Connection. Disconnect ();
You can write a line of log processing functions to track strophe logs and debug them. You can delete this line of comment and write your log processing in the function implementation. // Strophe. log = function (Level, MSG) {log ('Log: '+ MSG );}; The log () function used in this example:

Function log (MSG) {$ ('# log'). append (' <div> </div> '). append (document. createtextnode (MSG ));}

 

Directly output the content to a log Div on this page. Note: jquery syntax is used. In addition, you can define the rawinput and rawoutput functions to monitor the IO content on the connection. You can refer to the Code in the example.
Connection. rawinput = rawinput; connection. rawoutput = rawoutput ;... function rawinput (data) {log ('recv: '+ data);} function rawoutput (data) {log ('sent:' + data );}
In this example, the content sent and received is also output to the log. In addition, you can use the firebug plug-in of Firefox to debug JavaScript. Full running effect download from source // does todo Google docs not provide the file upload function? I need to find an address... Webimclient.zip

Reference manual [1] xep 0124 http://xmpp.org/extensions/xep-0124.html [2] The homepage for strophe is http://code.stanziq.com/strophe [3] strophe API Doc http://people.chesspark.com /~ Jack/strophe-preview/doc/files/strophe-js.html

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.