package com.rx.chart.common;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 公共属性
*
* @author Renxin
*
*/
public class Constants {
/**
* Session集合
*/
public static List users = new ArrayList();
/**
* 消息集合
*/
public static List messages = new ArrayList();
/**
* 会话线程映射
* Key:SessionId
* Value:Thread
*/
public static Map sessionThreadMapping = new HashMap();
}
2.SessionListener.java,监听Session的创建和销毁,维护会话列表.
package com.rx.chart.listener;
import static com.rx.chart.common.Constants.users;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.rx.chart.util.Util;
/**
* Session监听器
*
* @author Renxin
*
*/
public class SessionListener implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent httpsessionevent) {
// 加入到Session集合
users.add(httpsessionevent.getSession().getId());
// 唤醒全部更新列表
Util.wakeUpAllThread();
}
public void sessionDestroyed(HttpSessionEvent httpsessionevent) {
// 从Session集合移除
users.remove(httpsessionevent.getSession().getId());
// 唤醒全部更新列表
Util.wakeUpAllThread();
}
}
3.Initialization.java,获取用户列表和消息列表
package com.rx.chart.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rx.chart.util.Util;
/**
* 初始化
*
* @author Renxin
*
*/
public class Initialization extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 返回消息
Util.out(response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4.RefreshUserList.java,长连接刷新信息.
package com.rx.chart.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.rx.chart.common.Constants;
import com.rx.chart.util.Util;
/**
* 刷新
*
* @author Renxin
*
*/
public class RefreshUserList extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// 加入映射
Constants.sessionThreadMapping.put(session.getId(), Thread
.currentThread());
// 当前线程等待
try {
synchronized (Thread.currentThread()) {
Thread.currentThread().wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
// 返回消息
Util.out(response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
5.SendMessage,发送消息.
package com.rx.chart.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.rx.chart.common.Constants;
import com.rx.chart.util.Util;
/**
* 发送消息
*
* @author Renxin
*
*/
public class SendMessage extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String message = request.getParameter("message");
message = session.getId() + ":" + message;
Constants.messages.add(message);
// 唤醒全部更新列表
Util.wakeUpAllThread();
// 返回消息
Util.out(response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
6.Util,工具类.
package com.rx.chart.util;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import com.rx.chart.common.Constants;
import flexjson.JSONSerializer;
/**
* 工具类
*
* @author Renxin
*
*/
public class Util {
/**
* 唤醒全部
*/
public static void wakeUpAllThread() {
Iterator> iterator = Constants.sessionThreadMapping
.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
Thread thread = entry.getValue();
synchronized (thread) {
thread.notify();
}
}
}
/**
* 唤醒指定
*
* @param sessionId
*/
public static void wakeUpAllThread(String sessionId) {
Iterator> iterator = Constants.sessionThreadMapping
.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
if (sessionId.equals(entry.getKey())) {
Thread thread = entry.getValue();
synchronized (thread) {
thread.notify();
}
}
}
}
/**
* 输出用户列表
*
* @param response
* @throws IOException
*/
public static void out(HttpServletResponse response) throws IOException {
Map> map = new HashMap>();
map.put("users", Constants.users);
map.put("messages", Constants.messages);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(new JSONSerializer().serialize(map));
out.flush();
out.close();
}
}
7.页面
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>
首页用户列表
消息列表
//处理内容
function handlerContent(content) {
eval('var c=' + content);
if (c.users) {
var html;
html = '
- ';
for ( var u in c.users) {
html += '
';html += c.users[u];
html += '
';}
html += '
';$('#userList').html(html);
}
if (c.messages) {
var html;
html = '
- ';
for ( var m in c.messages) {
html += '
';html += c.messages[m];
html += '
';}
html += '
';$('#messageList').html(html);
}
}
//发消息
function subMsg() {
$.post('SendMessage', {
'message' : $('#message').val()
}, function(content) {
handlerContent(content);
});
$('#message').val('');
}
//初始化
function init() {
$.post('Initialization', function(content) {
handlerContent(content);
refreshUserList();
});
}
//刷新列表
function refreshUserList() {
$.post('RefreshUserList', function(content) {
handlerContent(content);
refreshUserList();
});
}
window.onload = init();
Chat.rar (85.6 KB)
下载次数: 276
分享到:
2012-05-30 16:15
浏览 10787
评论
7 楼
小鑫。
2014-11-14
KevinDai007 写道
小鑫。 写道
KevinDai007 写道
这个项目的用户列表是怎么会事?为什么我完全看不懂
哪个地方不懂?
我登录之后挂在哪里面,一会用户列表就变了,也就是sessionid变了,这是为什么
以前那个超时了?
6 楼
KevinDai007
2014-11-13
小鑫。 写道
KevinDai007 写道
这个项目的用户列表是怎么会事?为什么我完全看不懂
哪个地方不懂?
我登录之后挂在哪里面,一会用户列表就变了,也就是sessionid变了,这是为什么
5 楼
小鑫。
2014-11-12
KevinDai007 写道
这个项目的用户列表是怎么会事?为什么我完全看不懂
哪个地方不懂?
4 楼
KevinDai007
2014-11-12
这个项目的用户列表是怎么会事?为什么我完全看不懂
3 楼
小鑫。
2012-11-27
k11hao 写道
发送消息的速度特别快,那个pushlet用起来总是怪怪的
k11hao 写道
发送消息的速度特别快,那个pushlet用起来总是怪怪的
谢谢欣赏.
这个是写来玩的,pushlet那么多人用,可定有它的特别之处,我想应该是没调好吧,
2 楼
k11hao
2012-11-25
发送消息的速度特别快,那个pushlet用起来总是怪怪的
1 楼
k11hao
2012-11-25
我觉得这样写很不错