本文转自:http://htj1231825.iteye.com/blog/2206526
RemoteMessageServer 客服类?
Java代码??
class="star">
- package?com.gw.medical.hospital.utils.dwr;??
- ??
- import?java.util.Iterator;??
- import?java.util.Map;??
- ??
- import?javax.servlet.http.HttpSession;??
- ??
- import?org.directwebremoting.ScriptBuffer;??
- import?org.directwebremoting.ScriptSession;??
- import?org.directwebremoting.WebContextFactory;??
- import?org.directwebremoting.annotations.RemoteMethod;??
- import?org.directwebremoting.annotations.RemoteProxy;??
- ??
- @RemoteProxy(name?=?"RemoteMessageServer")??
- public?class?RemoteMessageServer?{??
- ??
- ?????
- ?
- ??
- ????@RemoteMethod??
- ????public?void?onPageLoad()?{??
- ????????getHttpSession().setAttribute("userInfo",?"CustomerService1");???
- ????????RemoteMessageConstant.getServerList().put(getScriptSession().getId(),?getScriptSession());??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ??
- ????@RemoteMethod??
- ????public?void?addMessage(final?String?message)?{??
- ????????HttpSession?httpsession?=?getHttpSession();??
- ????????String?clientName?=?httpsession.getAttribute("clientName")?==?null???""?:?httpsession.getAttribute("clientName").toString();??
- ????????ScriptSession?session?=?RemoteMessageConstant.getMapList().get(clientName);??
- ????????if?(session?!=?null)?{??
- ??????????????
- ????????????String?serverMessage=getHttpSession().getAttribute("userInfo").toString();??
- ????????????serverMessage=serverMessage+":"+message;??
- ????????????sendMessage(session,"receiveMessages",serverMessage);??
- ????????}??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ??
- ????@RemoteMethod??
- ????public?String?selectClient()?{??
- ????????addMessage("本次会话结束!感谢您的咨询!");??
- ??????????
- ????????removeClient();??
- ????????ScriptSession?sessionServer?=?getScriptSession();??
- ????????HttpSession?httpsession?=?getHttpSession();??
- ????????Map<String,?ScriptSession>?userMap?=?RemoteMessageConstant.getMapList();??
- ????????Iterator<String>?iterator?=?userMap.keySet().iterator();??
- ????????String?clientName?=?"";??
- ??
- ????????while?(iterator.hasNext())?{??
- ??????????????
- ????????????String?tempName?=?iterator.next();??
- ????????????if?(RemoteMessageConstant.getMappingList().get(tempName)?==?null)?{??
- ????????????????clientName?=?tempName;??
- ????????????????break;??
- ????????????}??
- ????????}??
- ????????ScriptSession?session?=?userMap.get(clientName);??
- ??????????
- ????????if?(session?!=?null)?{??
- ??????????????
- ????????????sendMessage(session,?"setServerName",?httpsession.getAttribute("userInfo").toString());??
- ????????????httpsession.setAttribute("clientName",?clientName);??
- ????????????RemoteMessageConstant.getMappingList().put(session.getId(),?sessionServer);??
- ????????????return?RemoteMessageConstant.getUserNamList().get(clientName);??
- ????????}??
- ??
- ????????return?"";??
- ????}??
- ??
- ?????
- ?
- ??
- ????public?void?removeClient()?{??
- ????????HttpSession?httpSession?=?getHttpSession();??
- ????????String?clientName?=?httpSession.getAttribute("clientName")?==?null???""?:?httpSession.getAttribute("clientName").toString();??
- ????????RemoteMessageConstant.getMappingList().remove(clientName);??
- ????????RemoteMessageConstant.getMapList().remove(clientName);??
- ????????RemoteMessageConstant.getUserNamList().remove(clientName);??
- ????????httpSession.removeAttribute("clientName");??
- ????}??
- ??
- ?????
- ?
- ?
- ??
- ????public?void?sendMessage(ScriptSession?scriptSession,?String?jsName,?String?message)?{??
- ????????ScriptBuffer?script?=?new?ScriptBuffer();??
- ????????script.appendCall(jsName,?message);??
- ????????scriptSession.addScript(script);??
- ????}??
- ??
- ?????
- ?
- ??
- ??
- ????public?ScriptSession?getScriptSession()?{??
- ??
- ????????return?WebContextFactory.get().getScriptSession();??
- ????}??
- ??
- ????public?HttpSession?getHttpSession()?{??
- ??
- ????????return?WebContextFactory.get().getSession();??
- ????}??
- ??
- }??
RemoteMessageClient? 客户端?
Java代码??
- package?com.gw.medical.hospital.utils.dwr;??
- ??
- import?java.util.Collection;??
- import?java.util.Iterator;??
- ??
- import?javax.servlet.ServletException;??
- import?javax.servlet.http.HttpSession;??
- ??
- import?org.directwebremoting.Browser;??
- import?org.directwebremoting.ScriptBuffer;??
- import?org.directwebremoting.ScriptSession;??
- import?org.directwebremoting.ScriptSessionFilter;??
- import?org.directwebremoting.WebContextFactory;??
- import?org.directwebremoting.annotations.RemoteMethod;??
- import?org.directwebremoting.annotations.RemoteProxy;??
- ??
- @RemoteProxy(name?=?"RemoteMessageClient")??
- public?class?RemoteMessageClient?{??
- ??
- ?????
- ?
- ??
- ????@RemoteMethod??
- ????public?void?onPageLoadClient()?{??
- ????????ScriptSession?scriptSession?=?getScriptSession();??
- ????????HttpSession?httpSession?=?getHttpSession();??
- ????????String?clientName?=?scriptSession.getId();??
- ????????httpSession.setAttribute("clientName",?clientName);??
- ????????RemoteMessageConstant.getMapList().put(clientName,?scriptSession);??
- ????????RemoteMessageConstant.getUserNamList().put(clientName,?"游客");??
- ??????????
- ????????showOnlinClient();??
- ????????DwrScriptSessionManagerUtil?dssm?=?new?DwrScriptSessionManagerUtil();??
- ????????try?{??
- ????????????dssm.init();??
- ????????}?catch?(ServletException?e)?{??
- ????????????e.printStackTrace();??
- ????????}??
- ????}??
- ??
- ?????
- ?
- ?
- ?
- ??
- ????@RemoteMethod??
- ????public?void?addMessageClient(String?message)?{??
- ????????HttpSession?httpSession?=?getHttpSession();??
- ????????String?clientName?=?httpSession.getAttribute("clientName")?==?null???""?:?httpSession.getAttribute("clientName").toString();??
- ????????String?autoMessage?=?message;??
- ????????ScriptSession?scriptSession?=?RemoteMessageConstant.getMappingList().get(clientName);??
- ??
- ????????if?(scriptSession?!=?null)?{??
- ????????????String?clientMessage=RemoteMessageConstant.getUserNamList().get(clientName);??
- ????????????clientMessage+=":"+autoMessage;??
- ????????????sendMessage(scriptSession,"receiveMessages",clientMessage);??
- ????????}??
- ????}??
- ??
- ?????
- ?
- ??
- ????public?void?showOnlinClient()?{??
- ????????Browser.withAllSessionsFiltered(new?ScriptSessionFilter()?{??
- ??????????????
- ????????????public?boolean?match(ScriptSession?session)?{??
- ????????????????if?(RemoteMessageConstant.getServerList().get(session.getId())?!=?null)?{??
- ????????????????????return?true;??
- ????????????????}?else?{??
- ????????????????????return?false;??
- ????????????????}??
- ????????????}??
- ????????},?new?Runnable()?{??
- ????????????private?ScriptBuffer?script?=?new?ScriptBuffer();??
- ??
- ????????????public?void?run()?{??
- ??????????????????
- ????????????????Iterator<String>?it?=?RemoteMessageConstant.getMapList().keySet().iterator();??
- ????????????????String?autoMessage?=?new?String();??
- ????????????????while?(it.hasNext())?{??
- ????????????????????String?key?=?it.next().toString();??
- ??????????????????????
- ????????????????????if?(RemoteMessageConstant.getMappingList().get(key)?==?null)?{??
- ??
- ????????????????????????autoMessage?+=?RemoteMessageConstant.getUserNamList().get(key)?+?";";??
- ????????????????????}??
- ????????????????}??
- ????????????????script.appendCall("showOnlinClient",?autoMessage);??
- ????????????????Collection<ScriptSession>?sessions?=?Browser.getTargetSessions();??
- ????????????????for?(ScriptSession?scriptSession?:?sessions)?{??
- ????????????????????scriptSession.addScript(script);??
- ????????????????}??
- ????????????}??
- ????????});??
- ????}??
- ??
- ??
- ?????
- ?
- ??
- ?????
- ?
- ?
- ?
- ?
- ??
- ????public?void?sendMessage(ScriptSession?scriptSession,?String?jsName,?String?message)?{??
- ????????ScriptBuffer?script?=?new?ScriptBuffer();??
- ????????script.appendCall(jsName,?message);??
- ????????scriptSession.addScript(script);??
- ????}??
- ??
- ????public?ScriptSession?getScriptSession()?{??
- ??
- ????????return?WebContextFactory.get().getScriptSession();??
- ????}??
- ??
- ????public?HttpSession?getHttpSession()?{??
- ??
- ????????return?WebContextFactory.get().getSession();??
- ????}??
- }??
DwrScriptSessionManagerUtil 监听?
Java代码??
- package?com.gw.medical.hospital.utils.dwr;??
- ??
- import?javax.servlet.ServletException;??
- ??
- import?org.directwebremoting.Container;??
- import?org.directwebremoting.ScriptBuffer;??
- import?org.directwebremoting.ScriptSession;??
- import?org.directwebremoting.ServerContextFactory;??
- import?org.directwebremoting.event.ScriptSessionEvent;??
- import?org.directwebremoting.event.ScriptSessionListener;??
- import?org.directwebremoting.extend.ScriptSessionManager;??
- import?org.directwebremoting.servlet.DwrServlet;??
- ??
- public?class?DwrScriptSessionManagerUtil?extends?DwrServlet?{??
- ??
- ????private?static?final?long?serialVersionUID?=?-7504612622407420071L;??
- ??
- ????public?void?init()?throws?ServletException?{??
- ????????Container?container?=?ServerContextFactory.get().getContainer();??
- ????????ScriptSessionManager?manager?=?container.getBean(ScriptSessionManager.class);??
- ????????ScriptSessionListener?listener?=?new?ScriptSessionListener()?{??
- ????????????public?void?sessionCreated(ScriptSessionEvent?ev)?{??
- ??
- ????????????}??
- ??
- ????????????public?void?sessionDestroyed(ScriptSessionEvent?ev)?{??
- ????????????????if?(RemoteMessageConstant.getMappingList().get(ev.getSession().getId())?!=?null)?{??
- ????????????????????ScriptSession?scriptSession?=?RemoteMessageConstant.getMappingList().get(ev.getSession().getId());??
- ????????????????????sendMessage(scriptSession,"receiveMessages","客户端已关闭!");??
- ????????????????}??
- ??????????????????
- ????????????????RemoteMessageConstant.getMapList().remove(ev.getSession().getId());??
- ????????????????RemoteMessageConstant.getMappingList().remove(ev.getSession().getId());??
- ????????????????RemoteMessageConstant.getServerList().remove(ev.getSession().getId());??
- ????????????????RemoteMessageConstant.getUserNamList().remove(ev.getSession().getId());??
- ??????????????????
- ????????????????RemoteMessageClient?rc?=?new?RemoteMessageClient();??
- ????????????????rc.showOnlinClient();??
- ????????????}??
- ????????};??
- ????????manager.addScriptSessionListener(listener);??
- ????}??
- ?????
- ?
- ?
- ??
- ????public?void?sendMessage(ScriptSession?scriptSession,?String?jsName,?String?message)?{??
- ????????ScriptBuffer?script?=?new?ScriptBuffer();??
- ????????script.appendCall(jsName,?message);??
- ????????scriptSession.addScript(script);??
- ????}??
- }??
RemoteMessageConstant 静态常量?
Java代码??
- package?com.gw.medical.hospital.utils.dwr;??
- ??
- import?java.util.HashMap;??
- import?java.util.Map;??
- ??
- import?org.directwebremoting.ScriptSession;??
- ??
- public?class?RemoteMessageConstant?{??
- ????private?static?Map<String,?ScriptSession>?mapList?=?new?HashMap<String,?ScriptSession>();??
- ????private?static?Map<String,?String>?userNamList?=?new?HashMap<String,?String>();??
- ????private?static?Map<String,?ScriptSession>?mappingList?=?new?HashMap<String,?ScriptSession>();??
- ????private?static?Map<String,?ScriptSession>?serverList?=?new?HashMap<String,?ScriptSession>();??
- ??
- ?????
- ?
- ??
- ????public?static?Map<String,?ScriptSession>?getMapList()?{??
- ????????return?mapList;??
- ????}??
- ??
- ????public?static?void?setMapList(Map<String,?ScriptSession>?mapList)?{??
- ????????RemoteMessageConstant.mapList?=?mapList;??
- ????}??
- ??
- ????public?static?Map<String,?ScriptSession>?getMappingList()?{??
- ????????return?mappingList;??
- ????}??
- ??
- ????public?static?void?setMappingList(Map<String,?ScriptSession>?mappingList)?{??
- ????????RemoteMessageConstant.mappingList?=?mappingList;??
- ????}??
- ??
- ????public?static?Map<String,?ScriptSession>?getServerList()?{??
- ????????return?serverList;??
- ????}??
- ??
- ????public?static?void?setServerList(Map<String,?ScriptSession>?serverList)?{??
- ????????RemoteMessageConstant.serverList?=?serverList;??
- ????}??
- ??
- ????public?static?Map<String,?String>?getUserNamList()?{??
- ????????return?userNamList;??
- ????}??
- ??
- ????public?static?void?setUserNamList(Map<String,?String>?userNamList)?{??
- ????????RemoteMessageConstant.userNamList?=?userNamList;??
- ????}??
- ??
- ??
- }??
serverMessage.jsp? 客服页?
Java代码??
- <%@?page?language="java"?contentType="text/html;?charset=utf-8"??pageEncoding="utf-8"%>??
- <!DOCTYPE?html?>??
- <html>??
- <head>??
- <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">??
- <title>Server</title>??
- <link?rel="stylesheet"?type="text/css"?href="<%=request.getContextPath()%>/css/themes/default/easyui.css">??
- <script?type="text/javascript"?src="<%=request.getContextPath()%>/dwr/engine.js">?</script>??
- <script?type='text/javascript'?src='<%=request.getContextPath()%>/dwr/util.js'></script>??
- <script?type="text/javascript"?src="<%=request.getContextPath()%>/dwr/interface/RemoteMessageServer.js">?</script>??
- <script?type="text/javascript"?src="<%=request.getContextPath()%>/js/jquery-1.8.3.min.js">?</script>??
- <script?type="text/javascript"?src="<%=request.getContextPath()%>/js/jquery.easyui.min.js">?</script>??
- <script?type="text/javascript">??
- ????$(function(){??
- ????????dwr.engine.setActiveReverseAjax(true);??
- ????????dwr.engine.setNotifyServerOnPageUnload(true);??
- ????????onPageLoad();??
- ??????????
- ????????$("#messageDialog").dialog({??
- ????????????title:'对话框',??
- ????????????width:600,??
- ????????????height:550,??
- ????????????closed:false,??
- ????????????cache:false??
- ????????});??
- ????});??
- ??????function?sendMessage(message)?{??
- ??????????????
- ????????????RemoteMessageServer.addMessage(message);??
- ????}??
- ??????
- ??????function?receiveMessages(messages)?{??
- ????????????var?p=$("??
- ");??
- ????????????$(p).append(messages);??
- ????????????$("#messageContent").append(p);??
- ??????}??
- ????????
- ????????
- ??????function?onPageLoad(){??
- ??????????
- ????????RemoteMessageServer.onPageLoad();??
- ???????}??
- ???????function?selectClient(){??
- ???????????RemoteMessageServer.selectClient({??
- ???????????????callback:function(clientName)?{???
- ??????????????????if(clientName=="")??
- ??????????????????????alert("选择失败!")??
- ??????????????????else??
- ?????????????????????$("#messageContent").append("客户:"+clientName+",已成功接入!"+"<br/>");??
- ?????????????????????$("#messageDialog").dialog({title:'客户:'+clientName});??
- ???????????????}??
- ?????????????????
- ???????????});??
- ???????}??
- ???????function?showOnlinClient(str){??
- ???????????$(".onlineUser").html(str);??
- ???????}??
- ?????????
- ?
- ??
- ?????function?showIcon(){??
- ????????if($("#icondiv").css("display")=="none"){??
- ????????????$("#icondiv").css("display","block");??
- ????????}else{??
- ????????????$("#icondiv").css("display","none");??
- ????????}??
- ?????}??
- ?????function?addIcon(str){??
- ?????????var?img=$('<img?src="<%=request.getContextPath()%>/images/dwr/'+str+'.gif"/>');??
- ?????????$("#messageTextArea").append($(img));??
- ?????}??
- ?????function?jsSendMessage(){??
- ?????????var?message=$("#messageTextArea").html();??
- ?????????$("#messageContent").append("我:"+message+"<br/>");??
- ?????????$("#messageTextArea").html("");??
- ?????????this.sendMessage(message);??
- ?????}??
- ??????????
- </script>??
- <style?type="text/css">??
- ????#icondiv?ul{??
- ????????width:?400px;???
- ????????height:?130px;??
- ????????overflow:?auto;??
- ????????border:?1px?solid?black;??
- ????}??
- ????#icondiv?ul?li{??
- ????????float:?left;??
- ????????width:?45px;??
- ????????list-style:?none;??
- ??????????
- ????}??
- ????#icondiv?ul?li?img{??
- ????????width:?40px;??
- ????????height:?40px;??
- ????}??
- </style>??
- </head>??
- <body>??
- ????[align=center;]??
- ????????????[url=javascript:void(0)]发送[/url]??
- ????????????[/align]??
- ????????</div>??
- ????</div>??
- ????<div><input?type="button"?onclick="selectClient()"?value="selectClient"/></div>??
- ????[align=center;]??
- ????????????[url=javascript:void(0)]发送[/url]??
- ????????????[/align]??
- ????????</div>??
- ????</div>??
- </body>??
- </html> ?