当前位置: 首页 > 工具软件 > Pushlets > 使用案例 >

pushlets 聊天室模型

宗政子辰
2023-12-01

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



 类似资料: