Use dwr3.0 to implement the service side to the browser to do message push, do scrolling comments or pop-up effect, and according to the video ID do push message interception function

Source: Internet
Author: User

The recent project to achieve video playback when doing the barrage and commentary scrolling, using flash do sockt programming will not, think of using server message push to do, look for data found using HTML5 WebSocket can be achieved, but IE8 is not support WebSocket, Finally determined to use DWR3 to do message push, ordinary DWR3 do message push will send the message to all open pages, so that a comment on a video will pop up to other videos, to achieve each video pop-up their comments, you need to do DWR3 message push filter processing, After a day of research, it's finally done.

Post the full code demo

1 configuration of Web. XML using DWR3

<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>

<init-param>
<param-name>fileUploadMaxBytes</param-name>
<param-value>25000</param-value>
</init-param>

<!--this should never is present in live--
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>

<init-param>
<param-name>accessLogLevel</param-name>
<param-value>runtimeexception</param-value>
</init-param>

<!--Remove This unless your want to use active reverse Ajax--
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>

<!--By default DWR creates application scope objects when they is first
Used. This creates them when the app-server is started--
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>

<!--warning:allowing JSON-RPC connections bypasses much of the security
Protection that DWR gives. If security is important-
<init-param>
<param-name>jsonRpcEnabled</param-name>
<param-value>true</param-value>
</init-param>

<!--warning:allowing JSONP connections bypasses much of the security
Protection that DWR gives. If security is important-
<init-param>
<param-name>jsonpEnabled</param-name>
<param-value>true</param-value>
</init-param>

<!--Data:urls is good for small images, but is slower, and could OOM for
Larger images. Leave this off (or keep ' false ') for anything but small images-
<init-param>
<param-name>preferDataUrlSchema</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

2 Configuration of the DWR.ML

<?xml version= "1.0" encoding= "UTF-8"?
<! DOCTYPE dwr Public "-//getahead limited//dtd Direct Web Remoting 3.0//en" "Http://getahead.org/dwr/dwr30.dtd";
<dwr>
    <allow>
        <create creator= "new" Scope= "Application";
          <param name= "class" value= " Com.example.dwr.reverseajax.JavascriptChat "/>
        </create>
        <create creator= "new" scope= "Application";
           <param Name= "class" Value= "Com.example.dwr.reverseajax.JavaChat"/>
        </create>
        <convert converter= "Bean" Match= "Com.example.dwr.reverseajax.Message"/>
   </allow>
</dwr>
3 Strust2 is used in the project and a property needs to be configured to strust2 the DWR request

<constant name= "Struts.action.excludePattern" value= "/dwr/*"/>

4 Java code

(1) Java files for receiving messages and processing messages


Package Com.example.dwr.reverseajax;

Import Java.util.HashMap;
Import java.util.LinkedList;
Import Java.util.Map;
Import Org.directwebremoting.Browser;
Import org.directwebremoting.ScriptSession;
Import Org.directwebremoting.ScriptSessionFilter;
Import org.directwebremoting.ScriptSessions;
Import Org.directwebremoting.WebContext;
Import Org.directwebremoting.WebContextFactory;

Import Com.jovision.modelBean.LoginInfo;

/**
*
* @author Liuhailong
*
*/
public class Javascriptchat
{

public void AddMessage (string text,final string oid)
{
String mess = text;
Webcontext WC = Webcontextfactory.get ();
Wc.getscriptsession (). SetAttribute ("OID", OID);
Logininfo Logininfo = (logininfo) wc.getsession (). getattribute ("Logininfo");
if (logininfo! = null) {
Mess = Logininfo.getaccount () + "said:" + mess;
}else{
Mess = "Visitor said:" + mess;
}
if (!map.containskey (OID)) {
linkedlist<message> list = new linkedlist<message> ();
Map.put (OID, list);
}
Final linkedlist<message> messages = Map.get (OID);
if (text! = null && Text.trim (). Length () > 0)
{
Messages.addfirst (new Message (mess));
while (Messages.size () > 10)
{
Messages.removelast ();
}
}
Filter filters
Scriptsessionfilter filter = new Scriptsessionfilter () {
Public boolean match (Scriptsession scriptsession) {
String tag = (string) scriptsession.getattribute ("OID");
return oid.equals (tag);
}
};
Execution method
Runnable run = new Runnable () {
public void Run () {
Scriptsessions.addfunctioncall ("Receivemessages", messages);
}
};
Send Message
Browser.withcurrentpagefiltered (Filter,run);
}

/*
* Map object that stores messages
*/
Private final map<string,linkedlist<message>> Map = new Hashmap<string,linkedlist<message>> ( );

}

(2) Message Object

Package Com.example.dwr.reverseajax;

/**
* @author Liuhailong
*/
public class Message
{
/**
* @param newtext the new message text
*/
Public Message (String NewText)
{
Text = NewText;

if (Text.length () > 256)
{
Text = text.substring (0, 256);
}
}

/**
* @return the message ID
*/
Public long GetId ()
{
return ID;
}

/**
* @return the message itself
*/
Public String GetText ()
{
return text;
}

/**
* When the message was created
*/
Private Long id = System.currenttimemillis ();

/**
* The text of the message
*/
private String text;
}

5 JSP page for message Popup

<%@ page language= "java" pageencoding= "UTF-8"%>
<%@ taglib uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
<%@ taglib prefix= "FN" uri= "Http://java.sun.com/jsp/jstl/functions"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>simple DWR Chat Version 3.0</title>
<script type= ' text/javascript ' src= '. /dwr/engine.js ' > </script>
<script type= ' text/javascript ' src= '. /dwr/interface/javascriptchat.js ' > </script>
<script type= ' text/javascript ' src= '. /dwr/util.js ' > </script>
<script type= "Text/javascript" src= ' javascript-chat.js ' > </script>
<body onload= "Init (); Tabs.init (' tabList ', ' tabcontents '); >
<div id= "Tabcontents" >
<div id= "Demodiv" >
<div id= "Chatlog" style= "Height:400px;overflow-y:auto" ></div>
<p>
Comments:
<input id= "text" onkeypress= "Dwr.util.onReturn (event, sendMessage)"/>
<input type= "button" value= "Send" onclick= "sendMessage ()"/>
</p>
</div>
</div>
</body>

6 using the Javescript script file code

function init () {
Dwr.engine.setActiveReverseAjax (TRUE);
}

function SendMessage () {
var text = dwr.util.getValue ("text");
var oid = window.parent.document.getElementById (' oid '). Value;
Dwr.util.setValue ("Text", "");
Javascriptchat.addmessage (text,oid);
}

function Receivemessages (Messages) {
var chatlog = "";
for (var data in messages) {
Chatlog = "<div>" + dwr.util.escapeHtml (messages[data].text) + "</div>" + chatlog;
}
Dwr.util.setValue ("Chatlog", Chatlog, {escapehtml:false});
}
When you do that, watch a blog on the web that compares DWR to the Web address: http://www.kankanews.com/ICkengine/archives/82552.shtml Click to open the link
The disadvantage of using this method to achieve message barrage is to compare the consumption of server resources, and hopefully there is a better way to provide

Use dwr3.0 to implement the service side to the browser to do message push, do scrolling comments or pop-up effect, and according to the video ID do push message interception function

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.