`

记录下自己写的最原始的聊天应用

阅读更多
用dwr实现,主要原理是用到了SessionScript

dwr的java部分:

package com.xinnuo.chat;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.directwebremoting.ScriptBuffer;
import org.directwebremoting.ScriptSession;
import org.directwebremoting.WebContext;
import org.directwebremoting.WebContextFactory;

import com.xinnuo.chat.invoke.ManageSessionScript;

public class Service {
	private  static  Map<String, ScriptSession> hm = new HashMap<String, ScriptSession>();
	

	
	public void sendMessage(String sender, String receiver, String message) {
	
		
		ScriptSession scriptSession=findScriptSession(receiver);
		System.out.println("scriptSession===="+scriptSession);
		ScriptBuffer scriptBuffer = new ScriptBuffer();
		System.out.println("写前");
		scriptBuffer.appendScript("go2(");
		scriptBuffer.appendData(message);
		scriptBuffer.appendScript(");");
		System.out.println("scriptBuffer==="+scriptBuffer);
		scriptSession.addScript(scriptBuffer);
		System.out.println("写后");
	}
	
public void setScriptSession(String id) {
	System.out.println("顾客登入");
	WebContext wc = WebContextFactory.get();
	
	ScriptSession scriptSession = wc.getScriptSession(); // 页面脚本
	System.out.println("scriptSession===="+scriptSession);	
	hm.put(id, scriptSession);
		System.out.println("顾客写入结束");		
	}


private ScriptSession findScriptSession(String userID) {
	
	return hm.get(userID);

}



}



服务端页面:
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>

<script language="javascript" src="/commonJs/jquery.js"></script>
<script type="text/javascript" src="/SyntInquiryJs/zuche/jquery.blockUI.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='/dwr/interface/KefuService.js'></script>


</script>
</HEAD>

<BODY>
 <input type="text" name="sessionId"  value="<%=request.getSession().getId()%>" />  
<input  id="cc" /><input type="button" onclick=go() />
<script>
function go(){
dwr.engine.setActiveReverseAjax(true);
DWREngine.setAsync(true);
KefuService.setScriptSession("66");
var cc=$("#cc").val();
KefuService.sendMessage("","55",cc);


}



</script>


</BODY>
</HTML>



客服端页面
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="javascript" src="/commonJs/jquery.js"></script>
<script type="text/javascript" src="/SyntInquiryJs/zuche/jquery.blockUI.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='/dwr/interface/KefuService.js'></script>
</HEAD>

<BODY>
 <input type="text" name="sessionId"  value="<%=request.getSession().getId()%>" />  
<script type="text/javascript">

function gg(){
alert("11");
dwr.engine.setActiveReverseAjax(true);
alert("33");
DWREngine.setAsync(true);
KefuService.setScriptSession("55");
alert("22");
}

function go2(a){
   $("#show").val(a);
  
}
</script>


<input id="show" />  <input type="button" onclick=gg() />



</BODY>
</HTML>
分享到:
评论
1 楼 yanxinfeng 2012-08-30  

相关推荐

Global site tag (gtag.js) - Google Analytics