DWR reverse Ajax server pushing technology

Source: Internet
Author: User

1. server push technology

The method by which a client sends a request to obtain server-side data is usually referred to as the "pull" technology. It clearly shows that the client is pulling server-side data, and sometimes the server needs to actively push data to the client, for example, when the number of online users of a chat is monitored and messages are sent to the online user, the background database is changed to actively update the display of all clients.

2. Reverse Ajax implements server pushing technology

Dwr2.x is also called DWR reverse Ajax (reverse Ajax). It is a technology that actively pushes data from the server to multiple browsers in the BS architecture. DWR's reverse Ajax mainly includes two modes: Active Mode and passive mode. The active mode includes polling and comet, while the passive mode only includes piggyback.

(1) Piggyback is the default mode.

If there is something in the background that needs to be pushed to the foreground, it is to wait until the next Ajax request is made on that page, append the content to be pushed to this request, and pass it back to the page. The changed content is transmitted back to the page only when the next request page is initiated.

(2) comet Mode

When the server establishes a connection with the browser and sends the page content to the browser, the corresponding connection is not closed, but is temporarily suspended. If there is any new content that needs to be pushed to the client, the data will be directly transferred through the pending connection. The number of connections provided by the server is certain. When a large number of suspended connections are not closed, new connection requests may not be accessible, thus affecting the service quality.

(3) Polling Mode

The browser regularly sends Ajax requests to the server and asks whether there is any content to be pushed in the background. If so, the server will return the pushed content. This method is similar to sending an Ajax request directly on the page through a timer, and then querying whether there is any change in content in the background. However, after using DWR, this part of work was completed by the framework.

3. Configure web. XML to use reverse Ajax

(1) piggyback. The default configuration of DWR in the web. xml file is piggyback.

(2) Add the following configurations to comet:

<Init-param>

<Param-Name> pollandcometenabled </param-Name>

<Param-value> true </param-value>

</Init-param>

<Init-param>

<Param-Name> activereverseajaxenabled </param-Name>

<Param-value> true </param-value>

</Init-param>

(3) Polling mode: Based on the comet mode, configure the following parameters:

<Init-param>

<Param-Name> org. directwebremoting. Extend. serverloadmonitor </param-nam

<Param-value> org. directwebremoting. impl. pollingserverloadmonitor </param-value>

</Init-param>

<! -- The number of milliseconds. The default request interval on the page is 5 seconds -->

<Init-param>

<Param-Name> disconnectedtime </param-Name>

<Param-value> 60000 </param-value>

</Init-param>

4. Write chat message processing

Public classMessagemanager {

Private StaticList <string>Msglist=NewRepeated list <string> ();

PublicList <string> getmsglist (){

Return Msglist;

}

Public synchronized voidSendmessage (string MSG ){

Msglist. Add (MSG );

Showmessage ();

}

Private voidShowmessage (){

Webcontext wctx = webcontextfactory.Get();

String currentpage = wctx. getcurrentpage ();

Scriptbuffer script =NewScriptbuffer (); script. appendscript ("receivemessages ("). appenddata (Msglist). Appendscript (");"); // call the JavaScript method on the page to add the value to the page.

Collection pages = wctx. getscriptsessionsbypage (currentpage); // loop all session pages and execute

For(Iterator it = pages. iterator (); it. hasnext ();){

Scriptsession othersession = (scriptsession) it. Next ();

Othersession. addscript (SCRIPT );

}

}

}

5. Configure the chat message processing class in DWR. xml

<Create creator ="New"Javascript ="Messagemanager"Scope ="Application">

<Param name ="Class"Value ="Service. messagemanager"/>

</Create>

6. Page code

<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en">

<HTML>

<Head>

<Title> reverseajax.html </title>

<Meta http-equiv ="Content-Type"Content ="Text/html;
Charset = UTF-8"
>

<SCRIPT type ='Text/JavaScript'Src ='/Dwrtest/DWR/interface/messagemanager. js'> </SCRIPT>

<SCRIPT type ='Text/JavaScript'Src ='/Dwrtest/DWR/engine. js'> </SCRIPT>

<SCRIPT type ='Text/JavaScript'Src ='/Dwrtest/DWR/util. js'> </SCRIPT>

<SCRIPT type ="Text/JavaScript">

DWR. Engine. setactivereverseajax (True);

FunctionReceivemessages (MSG ){

VaRMessage = "";

For(VaRI = 0; I <msg. length; I ++)

Message + = MSG [I] + "<br> ";

$ ("Content"). innerhtml = message

}

FunctionInit (){

Messagemanager. getmsglist (callback );

FunctionCallback (MSG ){

VaRMessage = "";

For(VaRI = 0; I <msg. length; I ++)

Message + = MSG [I] + "<br> ";

$ ("Content"). innerhtml = message

}

}

Window. onload = Init;

</SCRIPT>

</Head>

<Body>

<Input type ="Text"Id ="TXT"/>

<Button onclick = "messagemanager. sendmessage ($ ('txt '). Value)"> send </button>

<Div id ="Content"> </Div>

</Body>

</Html>

7. Effect

Send messages in one browser, and the other automatically obtains and displays the messages.


Reprint please indicate the source http://blog.csdn.net/shimiso

Technical Exchange Group: 173711587

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.