基于DWR推技術留言板

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.util.Logger;

/**
* @author Joe Walker [joe at getahead dot ltd dot uk]
*/
public class JavascriptChat
{
    /**
     * @param text The new message text to add
     */
    public void addMessage(String text)
    {
        if (text != null && text.trim().length() > 0)
        {
            messages.addFirst(new Message(text));
            while (messages.size() > 10)
            {
                messages.removeLast();
            }
        }

        WebContext wctx = WebContextFactory.get();
        String currentPage = wctx.getCurrentPage();

        ScriptBuffer script = new ScriptBuffer();
        script.appendScript("receiveMessages(")
              .appendData(messages)
              .appendScript(");");

        // Loop over all the users on the current page
        Collection pages = wctx.getScriptSessionsByPage(currentPage);
        for (Iterator it = pages.iterator(); it.hasNext();)
        {
            ScriptSession otherSession = (ScriptSession) it.next();
            otherSession.addScript(script);
        }
    }

    /**
     * The current set of messages
     */
    private LinkedList messages = new LinkedList();

    /**
     * The log stream
     */
    protected static final Logger log = Logger.getLogger(JavascriptChat.class);
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Simple DWR Chat Version 2.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>
<link rel="stylesheet" type="text/css" href="../generic.css" />
</head>

<body onload="init()">
<div id="page-title">[
<a href="http://getahead.org/dwr/">DWR Website</a> |
<a href="..">Web Application Index</a>
]</div>

<h1>Javascript Chat</h1>

<p>This is a very simple chat demo that uses reverse ajax to collect messages
and Javascript to manipulate the pages with the results.</p>

<p>
Your Message:
<input id="text" onkeypress="dwr.util.onReturn(event, sendMessage)"/>
<input type="button" value="Send" onclick="sendMessage()"/>
</p>
<hr/>

<div id="chatlog"></div>

</body>
</html>


function init() {
dwr.engine.setActiveReverseAjax(true);
}

function sendMessage() {
var text = dwr.util.getValue("text");
dwr.util.setValue("text", "");
JavascriptChat.addMessage(text);
}

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 });
}

你可能感兴趣的:(JavaScript,Web,Ajax,XHTML,DWR)