DWR push technology

Source: Internet
Author: User
Document directory
  • 1. receive page of push message
  • 2. Java class responsible for pushing messages
  • 3. Remaining work

The dwr2.0.x push technology is used to push and send messages. It is suitable for chatroom, stock information display, and other scenarios. It is better than the current JS regular polling Server policy, this greatly saves resources on the server side (no need to respond to the scheduled query of the client when no data changes ).

At, the system was able to operate normally. (Note that 2.0m3 needs to be downloaded from the http://dwr.dev.java.net)

The classic example of pushing technology is the chatroom in DWR examples. In addition, the scenario in springside is very real.

1. receive page of push message
1. <SCRIPT type = 'text/JavaScript 'src = '<C: URL value = "/DWR/engine. JS "/> '> </SCRIPT> <SCRIPT type = 'text/JavaScript 'src =' <C: URL value ="/DWR/interface/ordernotice. JS "/> '> </SCRIPT> <SCRIPT type = 'text/JavaScript' src = '<C: URL value ="/DWR/util. JS "/> '> </SCRIPT> 2. <script language = "JavaScript"> function receivemessages (ID) {$ ('ordernotice '). innerhtml = "receive a new order with ID +! "; $ ('Ordernotice '). show () ;}</SCRIPT> 3. <body onload = "dwrengine. setreverseajax (true); "> <Div id =" ordernotice "/>

First, introduce DWR and the Js of the Java class (ordernotice) responsible for pushing operations.

Second, set any JS function for receiving push information

Third, set reverseajax = true in onload of the body to start listening for information.

For details, see springside-bookstore \ webapp \ admin \ top. jsp.

2. Java class responsible for pushing messages
 public class OrderNotice {    public void noticeNewOrder(int id) {        WebContext wctx = WebContextFactory.get();        ScriptBuffer script = new ScriptBuffer();        script.appendScript("receiveMessages(")                .appendData(id)                .appendScript(");");
        ServerContext sctx = ServerContextFactory.get(wctx.getServletContext());        Collection<ScriptSession> pages = sctx.getScriptSessionsByPage("/springside/admin/top.jsp");        for (ScriptSession session : pages) {            session.addScript(script);        }    }}

It can be seen that scriptbuffer is used to construct an SQL statement executed on the client, and webcontext is used to locate the session to be sent for sending. Note that the URL path of the hardcode is used to determine the subscriber. You can also use currenpage to send the session to the same page as the ordernotice caller.

3. Remaining work

Set DWR. xml and call ordernotice from the client in the old DWR mode.

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.