论坛首页 Web前端技术论坛

DWR 2.0 Web聊天的ScriptSession 长连接问题

浏览 7038 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-18  
DWR
我的方法很简单从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>

   发表时间:2008-09-18  
用户发请求到服务器端时,即更新存储在session中的scriptsession  id,怎么样?
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics