pushlets 聊天室模型
来源:互联网 发布:我国数据安全保护策略 编辑:程序博客网 时间:2024/05/28 15:28
使用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 onclick=send();>send</button></div><div id="ptp" class="ptp"><input id="pin" type="text" /><button onclick=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;@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// TODO Auto-generated method stubif ("m".equals(req.getParameter("type"))) {mul(req);} else if ("p".equals(req.getParameter("type"))) {uni(req);}}// multicastprivate 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);}// unicastprivate 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);}}
- pushlets 聊天室模型
- Pushlets & Streamlets
- 聊天室UML模型一致性问题
- u3d聊天室模型。(转载)
- pushlets技术-开发日记1
- Comet框架Pushlets的集成
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- 聊天室
- POJ 1733 并查集 偏移向量
- 防止rm误操作
- 【tips】网站应用架构
- hibernate注解ID自增长
- VC++ 中实现进制2进制,10进制,16进制的相互转换
- pushlets 聊天室模型
- C语言中几个关键字(inline,volatile,static)
- 通过实现 Filter 接口进行项目全局编码控制
- hibernate annotation
- c程序学习笔记
- 在vs2010或者vs2008中配置PC-lint9.0版
- 转载和积累系列 - Jquery 表单验证类
- java工厂模式
- iOS 5基础教程