Detailed steps for precise message push using dwr3

Source: Internet
Author: User

Detailed steps for precise message push using dwr3

Recently, the project needs to use push messages. After a long time, I finally found a good article, so that I can repost it later and share it with you, hoping to help people who need it.

 

1. Introduce dwr. jar in the project and configure it in web. xml as follows:

<Servlet>

<Servlet-name> dwr-invoker </servlet-name>

<Servlet-class>

Org. directwebremoting. servlet. DwrServlet

</Servlet-class>

<Init-param>

<Param-name> crossDomainSessionSecurity </param-name>

<Param-value> false </param-value>

</Init-param>

<Init-param>

<Param-name> allowScriptTagRemoting </param-name>

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

</Init-param>

<Init-param>

<Param-name> classes </param-name>

<Param-value> java. lang. Object </param-value>

</Init-param>

<Init-param>

<Param-name> activeReverseAjaxEnabled </param-name>

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

</Init-param>

<Init-param>

<Param-name> initApplicationScopeCreatorsAtStartup </param-name>

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

</Init-param>

<Init-param>

<Param-name> maxWaitAfterWrite </param-name>

<Param-value> 3000 </param-value>

</Init-param>

<Init-param>

<Param-name> debug </param-name>

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

</Init-param>

<Init-param>

<Param-name> logLevel </param-name>

<Param-value> WARN </param-value>

</Init-param>

</Servlet>

2. Create a dwr. xml file in the directory of the same level as web. xml. The content is as follows:

<! DOCTYPE dwr PUBLIC

"-// GetAhead Limited // DTD Direct Web Remoting 3.0 // EN"

Http://getahead.org/dwr/dwr30.dtd>

<Dwr>

<Alow>

<Create creator = "new" javascript = "MessagePush">

<Param name = "class" value = "com. huatech. messageremind. service. MessagePush"/>

</Create>

</Alow>

</Dwr>

This is the basic configuration of dwr, MessagePush is used in javascript on the page, com. huatech. messageremind. service. messagePush implements the method to be called. MessagePush I think is equivalent to a ing in the java class, And MessagePush is used in javascript. the method implemented in the java class can be called in dwr.

Third, to use dwr, introduce the script to the page you want to push,

<Script type = "text/javascript" src = "<% = basepath %> dwr/engine. js"> </script>

<Script type = "text/javascript" src = "<% = basepath %> dwr/util. js"> </script>

<Script type = "text/javascript" src = "<% = basepath %> dwr/interface/MessagePush. js"> </script>

We can see that the javascript configured in dwr. xml is also introduced. engine. js and util. js must be introduced.

All the above three items are basic configurations. You have to do this if you want to use dwr.

Fourth, implement precise message push

Message push is simple, but some other operations are required to implement precise push.

1. When logging on to a user, you must put the userId or other unique identifiers in the session. I put the userId,

Here we use userId as the unique identifier.

2. When loading the page you want to push, You Need To onload a method that I implement in the MessagePush class. Of course, you need to use dwr to call

The js call method is as follows:

Function onPageLoad (){

Var userId = '$ {userinfo. humanid }';

MessagePush. onPageLoad (userId );

}

<Body onload = "dwr. engine. setActiveReverseAjax (true); dwr. engine. setpolicyserveronpageunload (true); onPageLoad ();> all three functions in onload are required, among which dwr. engine. setActiveReverseAjax (true); dwr. engine. setpolicyserveronpageunload (true); is a function in dwr.

The implementation methods in the MessagePush class are as follows:

Public void onPageLoad (String userId ){

ScriptSession scriptSession = WebContextFactory. get (). getScriptSession ();

ScriptSession. setAttribute (userId, userId );

DwrScriptSessionManagerUtil dwrScriptSessionManagerUtil = new DwrScriptSessionManagerUtil ();

Try {

DwrScriptSessionManagerUtil. init ();

} Catch (ServletException e ){

E. printStackTrace ();

}

}

We have noticed that the onPageLoad method also has a class named DwrScriptSessionManagerUtil, which is implemented as follows:

Public class DwrScriptSessionManagerUtil extends DwrServlet {

Private static final long serialVersionUID =-7504612622407420071L;

 

Public void init ()

Throws ServletException {

 

 

Container container = ServerContextFactory. get (). getContainer ();

 

ScriptSessionManager manager = container

. GetBean (ScriptSessionManager. class );

 

ScriptSessionListener listener = new ScriptSessionListener (){

 

Public void sessionCreated (ScriptSessionEvent ev ){

 

HttpSession session = WebContextFactory. get (). getSession ();

 

String userId = (User) session. getAttribute ("userinfo"). getHumanid () + "";

System. out. println ("a ScriptSession is created! ");

Ev. getSession (). setAttribute ("userId", userId );

 

}

 

Public void sessionDestroyed (ScriptSessionEvent ev ){

System. out. println ("a ScriptSession is distroyed ");

}

 

};

 

Manager. addScriptSessionListener (listener );

 

}

 

}

The fourth step is the most important. For the fourth step I have studied for more than two days, next I will start pushing messages.

Fifth, message push

Call the following method when you want to push messages:

Public void sendMessageAuto (String userid, String message ){

Final String userId = userid;

Final String autoMessage = message;

Browser. withAllSessionsFiltered (new ScriptSessionFilter (){

Public boolean match (ScriptSession session ){

If (session. getAttribute ("userId") = null)

Return false;

Else

Return (session. getAttribute ("userId"). equals (userId );

}

}, New Runnable (){

Private ScriptBuffer script = new ScriptBuffer ();

Public void run (){

Script. appendCall ("showMessage", autoMessage );

Collection <ScriptSession> sessions = Browser

. GetTargetSessions ();

For (ScriptSession scriptSession: sessions ){

ScriptSession. addScript (script );

}

}

});

}

Userid is the person you want to push the message to, and the message you want to push. You have noticed script. appendCall ("showMessage", autoMessage) here );

ShowMessage is the javascript method on the page to be pushed. autoMessage is the parameter of this method, so that the page can get the pushed content, it depends on your needs.

 

So far, a precise push step for dwr messages has been completed. In fact, many things are not difficult, but we don't know how to use them.

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.