我的方法很简单从js 里面取sessionid 传到server里,
发送消息的时候做了一个选择排序 根据session建立时间重新排序了一下,
将最新的scriptsession 返回,
虽然现在可以解决scriptsession 问题,但是估计还是会存在问题,而且感觉方法很笨..
请问谁有更好的办法可以很好的解决这个问题? 谢谢大家了!
ChatManager.java
package test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.ServerContext;
import org.directwebremoting.ServerContextFactory;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.proxy.dwr.Util;
/**
* DWR ChatTest
*
* @author 李斌
*
*/
public class ChatManager {
public ScriptSession session;
public static List<User> users = new ArrayList<User>();
public void getTest(){
System.out.println("##test");
}
public String addUser(String username ,HttpServletRequest request){
User u = new User(request.getSession().getId(), username);
users.add(u);
this.setScriptSessionFlag(u.getUserid());
System.out.println("加入用户"+username);
System.out.println("Session Id"+u.getUserid());
return u.getUserid();
}
public String updateUserList(HttpServletRequest request){
//获得DWR上下文
ServletContext sc = request.getSession().getServletContext();
ServerContext sctx = ServerContextFactory.get(sc);
//获得当前浏览 index.jsp 页面的所有脚本session
Collection sessions = sctx.getScriptSessionsByPage("/testchat/index.jsp");
Util util = new Util(sessions);
//处理这些页面中的一些元素
util.removeAllOptions("receiver");
util.addOptions("receiver", users,"userid","username");
return null;
}
public void setScriptSessionFlag(String userid) {
WebContextFactory.get().getScriptSession().setAttribute("userid", userid);
}
ScriptSession s = null;
public void getPageScriptSession(String sessionid){
WebContextFactory.get().getScriptSession().setAttribute("userid", sessionid);
s =(ScriptSession) WebContextFactory.get().getScriptSession();
}
public void sendMsg(String sender,String receiverid,String msg,HttpServletRequest request){
session = this.getScriptSession(receiverid, request);
Util util = new Util(session);
util.setValue("sender", sender);
util.setValue("msg", msg);
}
public ScriptSession getScriptSession(String userid, HttpServletRequest request) {
ScriptSession scriptSessions = null;
//Collection<ScriptSession> sessions = new LinkedHashSet<ScriptSession>();
Collection<ScriptSession> sessions = new HashSet<ScriptSession>();
sessions.addAll(ServerContextFactory.get(request.getSession().getServletContext())
.getScriptSessionsByPage("/testchat/index.jsp"));
List<ScriptSession> result = new ArrayList<ScriptSession>();
for (ScriptSession session : sessions) {
String xuserid = (String) session.getAttribute("userid");
if (xuserid != null && xuserid.equals(userid)) {
result.add(session);
}
}
ComparatorList comparator=new ComparatorList();
java.util.Collections.sort(result,comparator);
/*for(int i = 0 ; i<result.size();i++){
for(int j = i+1;j<result.size();j++){
if(result.get(i).getCreationTime()<result.get(j).getCreationTime()){
java.util.Collections.swap(result, i, j);
ScriptSession tem = result.get(i);
result.set(i, result.get(j));
result.set(j, tem);
}
}
}*/
return result.iterator().next();
}
}
class ComparatorList implements Comparator{
public int compare(Object arg0, Object arg1) {
ScriptSession ss0=(ScriptSession)arg0;
ScriptSession ss1=(ScriptSession)arg1;
if(ss0.getCreationTime()>ss1.getCreationTime()){
return -1;
}
else {
return 1;
}
}
}
test.js
var Chat ={version:"1.0"}
/**
* ChatTest Js
*
* @author 李斌
*
*/
Chat.getUserList = function(){ ChatManager.getTest()}
Chat.getInit = function(){
var userid = document.getElementById('userid').value;
dwr.engine.setActiveReverseAjax(true); // 激活反转 重要
ChatManager.updateUserList();
ChatManager.getPageScriptSession(document.getElementById('userid').value);
}
Chat.addUser = function(){
var username = document.getElementById('username').value;
ChatManager.addUser(username,addUsercallback);
}
Chat.sendMsg = function(){
var message = document.getElementById('message').value;
var receiver = document.getElementById('receiver');
var selectedid = receiver.options[receiver.selectedIndex].value;
selectedname = receiver.options[receiver.selectedIndex].text;
ChatManager.sendMsg(document.getElementById('username').value,selectedid,message);
}
function addUsercallback(data){
ChatManager.updateUserList();
document.getElementById('userid').value = data
}
Chat.getScriptSession = function(){
ChatManager.getPageScriptSession(document.getElementById('userid').value);
}
function getSession(data){
alert(data)
}
window.onload = Chat.getInit();
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<input type="hidden" id="userid" name="userid" value="<%=session.getId()%>">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<script type='text/javascript' src='dwr/interface/ChatManager.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type="text/javascript" src="test.js"></script>
</head>
<body>
session id : <%=session.getId()%><br>
<input type="text" id="username"><input onclick="Chat.addUser()" type="button" value="加入聊天">
<br>
<select name="receiver" id="receiver"></select>
<input type="text" id="message"><input onclick="Chat.sendMsg()" type="button" value="发送">
<br><input type="text" id="sender">
<input type="text" id="msg">
<br>
<!-- input type="button" onclick="Chat.getScriptSession()" value="重新连接" -->
</body>
</html>
分享到:
相关推荐
总的来说,DWR后台推送技术通过长连接或者定期轮询实现了服务器到客户端的数据实时推送,为Web应用提供了类似桌面应用的交互体验。了解并掌握DWR的推送机制,以及在不同版本和框架下的应用,对于提升Web应用的实时性...
Direct Web Remoting (DWR) 是一种开源框架,它简化了JavaScript与服务器端Java代码之间的交互。DWR 提供了一种简单的方法来将 Java 对象暴露给 JavaScript,允许 JavaScript 调用 Java 方法,以及让 Java 方法调用 ...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答