pushlets 聊天室模型

使用Test类处理上传的数据 并使用pushlets的dispatcher推送消息

聊天页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML>
<html>
	<head>
		<base href="<%=basePath%>">
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<title>chat</title>
		<script type="text/javascript" src="ajax-pushlet-client.js"></script>
		<script type="text/javascript">
	
<%out.write("room=" + (new Random()).nextInt(1));%>
	PL._init();
	PL.joinListen("chat" + room);

	function onData(event) {
		var line = event.get("ctnt");
		card(line);
	}
	ajax = new XMLHttpRequest();
	function send() {
		var text = document.getElementById("in").value;
		ajax.open("POST", "chat", true);
		ajax.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		ajax.send("type=m&id=" + PL.sessionId + "&text=" + text + "&room="
				+ room);
	}
	function card(line) {
		var ctnts = line.split(":");
		var wrapper = document.createElement("div");
		wrapper.setAttribute("class", "card");
		var id = document.createElement("div");
		var text = document.createElement("div");
		id.addEventListener("click", function(event) {
			ptp(id, event);
		}, true);
		id.setAttribute("class", "id");
		id.innerHTML = ctnts[0];
		text.setAttribute("class", "text");
		text.innerHTML = ctnts[1];
		wrapper.appendChild(id);
		wrapper.appendChild(text);
		document.getElementById("msg").appendChild(wrapper);
	}
	function ptp(div, event) {
		var ptpWin = document.getElementById("ptp");
		ptpWin.style.display = "block";
		ptpWin.style.left = event.clientX - 10 + "px";
		ptpWin.style.top = event.clientY - 10 + "px";
		ptpWin.tar = div.innerHTML;
	}
	function psend(ptpWin) {
		var text = document.getElementById("pin").value;
		ajax.open("POST", "chat", true);
		ajax.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		ajax.send("type=p&id=" + PL.sessionId + "&text=" + text + "&room="
				+ room + "&person=" + ptpWin.tar);
		ptpWin.style.display = "none";
	}
</script>
		<style type="text/css">
.ptp {
	display: none;
	position: absolute;
}

.card {
	width: 95%;
	-webkit-border-radius: 3px;
}

.id {
	width: 18%;
	padding: 1px;
	float: left;
	clear: left;
	background-color: #E0A0E0;
	-webkit-border-radius: 3px;
	word-break: break-all;
}

.text {
	width: 80%;
	float: left;
	clear: right;
}

#msg {
	max-width: 24em;
	height: 80%;
	max-height: 40em;
	border: 2px solid #98bf21;
	overflow-y: scroll;
	background-color: #C0E0E0;
}
</style>
	</head>

	<body>
		<div style="width: 24em;">
			<div id="msg"></div>
			<div style="float: right;">
				<input id="in" type="text" />
				<button οnclick=send();>
					send
				</button>
			</div>
			<div id="ptp" class="ptp">
				<input id="pin" type="text" />
				<button οnclick=psend(this.parentNode);>
					send
				</button>
			</div>
		</div>
	</body>
</html>
处理事务的后台类

package gt.chat;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nl.justobjects.pushlet.core.Dispatcher;
import nl.justobjects.pushlet.core.Event;

public class Test extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3671469888301952266L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		if ("m".equals(req.getParameter("type"))) {
			mul(req);
		} else if ("p".equals(req.getParameter("type"))) {
			uni(req);
		}
	}

	// multicast
	private void mul(HttpServletRequest arg0) {
		String id = arg0.getParameter("id");
		String text = arg0.getParameter("text");
		String room = arg0.getParameter("room");
		Event e = Event.createDataEvent("chat" + room);
		e.setField("ctnt", id + ":" + text);
		Dispatcher.getInstance().multicast(e);
	}

	// unicast
	private void uni(HttpServletRequest arg0) {
		String id = arg0.getParameter("id");
		String text = arg0.getParameter("text");
		String person = arg0.getParameter("person");
		String room = arg0.getParameter("room");
		Event e = Event.createDataEvent("chat" + room);
		e.setField("ctnt", id + ":" + text);
		Dispatcher.getInstance().unicast(e, person);
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值