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.