DWR3rc1 简单聊天室实例

dwr.xml 和 web.xml 不再多说,网上很多

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);
}
});
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值