dwr.xml 和 web.xml 不再多说,网上很多
jsp:
Java
jsp:
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/include/global.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>Insert title here</title>
<script src="${pageContext.request.contextPath}/dwr/interface/SynMsg.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/dwr/engine.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/dwr/util.js" type="text/javascript"></script>
<script type="text/javascript">
function init() {
dwr.engine.setActiveReverseAjax(true);
}
function sendMessage() {
SynMsg.addMessage(dwr.util.getValue("producer"),dwr.util.getValue("consumer"),dwr.util.getValue("text"));
}
function receiveMessages(messages) {
var consumer = "${appConext_userName}";
var temp;
for (var data in messages) {
if(messages[data].consumer ==consumer && messages[data].text != ''){
temp = " " + dwr.util.escapeHtml(messages[data].text) + "" + temp;
}
}
dwr.util.setValue("chatlog", temp, { escapeHtml:false });
}
</script>
</head>
<body onload="dwr.engine.setActiveReverseAjax(true);sendMessage();">
<div id="demoDiv">
<p>
<input type="hidden" name="producer" value="${appConext_userName}"/>
consumer: <input id="consumer" name="consumer" />
Your Message:
<input id="text" onkeypress="dwr.util.onReturn(event, sendMessage)"/>
<input type="button" value="Send" onclick="sendMessage()"/>
</p>
<hr/>
<ul id="chatlog" style="list-style-type:none;">
</ul>
</div>
</body>
</html>
Java
public class SynMsg {
public static LinkedList<Msg> msgs = new LinkedList<Msg>();
/**
* @param text
* The new message text to add
*/
public void addMessage(String producer,String consumer,String text) {
// Make sure we have a list of the list 10 messages
if (StringUtils.isNotBlank(text)) {
System.out.println(producer+"--"+consumer+"--"+text);
msgs.addFirst(new Msg(producer, consumer, text));
while (msgs.size() > 10) {
msgs.removeLast();
}
}
// Clear the input box in the browser that kicked off this page only
Util.setValue("text", "");
// For all the browsers on the current page:
Browser.withCurrentPage(new Runnable() {
public void run() {
// Clear the list and add in the new set of messages
// Util.removeAllOptions("chatlog");
// Util.addOptions("chatlog", subMsgs("sgy"), "text");
ScriptSessions.addFunctionCall("receiveMessages", msgs);
}
});
}