基于DWR的点对点聊天实现 server---client_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 基于DWR的点对点聊天实现 server---client

基于DWR的点对点聊天实现 server---client

 2015/4/28 18:15:38  jsczxy2  程序员俱乐部  我要评论(0)
  • 摘要:本文转自:http://htj1231825.iteye.com/blog/2206526RemoteMessageServer客服类Java代码packagecom.gw.medical.hospital.utils.dwr;importjava.util.Iterator;importjava.util.Map;importjavax.servlet.http.HttpSession;importorg.directwebremoting.ScriptBuffer;importorg
  • 标签:Server 实现 client DWR

本文转自:http://htj1231825.iteye.com/blog/2206526

RemoteMessageServer 客服类?

Java代码??收藏代码class="star">
  1. package?com.gw.medical.hospital.utils.dwr;??
  2. ??
  3. import?java.util.Iterator;??
  4. import?java.util.Map;??
  5. ??
  6. import?javax.servlet.http.HttpSession;??
  7. ??
  8. import?org.directwebremoting.ScriptBuffer;??
  9. import?org.directwebremoting.ScriptSession;??
  10. import?org.directwebremoting.WebContextFactory;??
  11. import?org.directwebremoting.annotations.RemoteMethod;??
  12. import?org.directwebremoting.annotations.RemoteProxy;??
  13. ??
  14. @RemoteProxy(name?=?"RemoteMessageServer")??
  15. public?class?RemoteMessageServer?{??
  16. ??
  17. ????/**?
  18. ?????*服务端初始化?
  19. ?????*/??
  20. ????@RemoteMethod??
  21. ????public?void?onPageLoad()?{??
  22. ????????getHttpSession().setAttribute("userInfo",?"CustomerService1");?//?测试用,存入模拟userInfo信息??
  23. ????????RemoteMessageConstant.getServerList().put(getScriptSession().getId(),?getScriptSession());//?存储当前客服,到在线客服列表??
  24. ????}??
  25. ??
  26. ????/**?
  27. ?????*?服务端发送信息?
  28. ?????*??
  29. ?????*?@param?message?
  30. ?????*/??
  31. ????@RemoteMethod??
  32. ????public?void?addMessage(final?String?message)?{??
  33. ????????HttpSession?httpsession?=?getHttpSession();//?获取session??
  34. ????????String?clientName?=?httpsession.getAttribute("clientName")?==?null???""?:?httpsession.getAttribute("clientName").toString();//?当前客户端ID??
  35. ????????ScriptSession?session?=?RemoteMessageConstant.getMapList().get(clientName);//?根据clientName获取scriptSession;??
  36. ????????if?(session?!=?null)?{??
  37. ????????????//?发送消息??
  38. ????????????String?serverMessage=getHttpSession().getAttribute("userInfo").toString();??
  39. ????????????serverMessage=serverMessage+":"+message;??
  40. ????????????sendMessage(session,"receiveMessages",serverMessage);??
  41. ????????}??
  42. ????}??
  43. ??
  44. ????/**?
  45. ?????*?选择下一个客户端?
  46. ?????*??
  47. ?????*?@return?boolean?
  48. ?????*/??
  49. ????@RemoteMethod??
  50. ????public?String?selectClient()?{??
  51. ????????addMessage("本次会话结束!感谢您的咨询!");??
  52. ????????//?销毁上一位客户??
  53. ????????removeClient();??
  54. ????????ScriptSession?sessionServer?=?getScriptSession();//?获取客服ScriptSsession??
  55. ????????HttpSession?httpsession?=?getHttpSession();//?获取session??
  56. ????????Map<String,?ScriptSession>?userMap?=?RemoteMessageConstant.getMapList();//?用户在线MAP??
  57. ????????Iterator<String>?iterator?=?userMap.keySet().iterator();??
  58. ????????String?clientName?=?"";//?获取客户端列表中的,clientName;??
  59. ??
  60. ????????while?(iterator.hasNext())?{??
  61. ????????????//?判断当前用户是否有客服接入,如未接入,接入该客户??
  62. ????????????String?tempName?=?iterator.next();??
  63. ????????????if?(RemoteMessageConstant.getMappingList().get(tempName)?==?null)?{??
  64. ????????????????clientName?=?tempName;??
  65. ????????????????break;??
  66. ????????????}??
  67. ????????}??
  68. ????????ScriptSession?session?=?userMap.get(clientName);//?根据clientName获取scriptSession;??
  69. ????????//?更改客户端的服务ID??
  70. ????????if?(session?!=?null)?{??
  71. ????????????//?调用页面JS??
  72. ????????????sendMessage(session,?"setServerName",?httpsession.getAttribute("userInfo").toString());??
  73. ????????????httpsession.setAttribute("clientName",?clientName);//?存储当前客户ID??
  74. ????????????RemoteMessageConstant.getMappingList().put(session.getId(),?sessionServer);//?存储对应关系,到关系MAP??
  75. ????????????return?RemoteMessageConstant.getUserNamList().get(clientName);??
  76. ????????}??
  77. ??
  78. ????????return?"";??
  79. ????}??
  80. ??
  81. ????/**?
  82. ?????*?销毁当前,服务端对应的客户端信息?
  83. ?????*/??
  84. ????public?void?removeClient()?{??
  85. ????????HttpSession?httpSession?=?getHttpSession();//?获取httpsession??
  86. ????????String?clientName?=?httpSession.getAttribute("clientName")?==?null???""?:?httpSession.getAttribute("clientName").toString();??
  87. ????????RemoteMessageConstant.getMappingList().remove(clientName);//?从关系Map中,删除对应关系。??
  88. ????????RemoteMessageConstant.getMapList().remove(clientName);//?从用户Map中,删除该用户;??
  89. ????????RemoteMessageConstant.getUserNamList().remove(clientName);//从用户名列表?删除该用户名??
  90. ????????httpSession.removeAttribute("clientName");//?删除当前服务端的,客户ID??
  91. ????}??
  92. ??
  93. ????/**?
  94. ?????*?调用页面JS?
  95. ?????*?@param?scriptSession,jsName:JS方法名,message:发送的信息?
  96. ?????*/??
  97. ????public?void?sendMessage(ScriptSession?scriptSession,?String?jsName,?String?message)?{??
  98. ????????ScriptBuffer?script?=?new?ScriptBuffer();??
  99. ????????script.appendCall(jsName,?message);??
  100. ????????scriptSession.addScript(script);??
  101. ????}??
  102. ??
  103. ????/**?
  104. ?????*?get?set?
  105. ?????*/??
  106. ??
  107. ????public?ScriptSession?getScriptSession()?{??
  108. ??
  109. ????????return?WebContextFactory.get().getScriptSession();??
  110. ????}??
  111. ??
  112. ????public?HttpSession?getHttpSession()?{??
  113. ??
  114. ????????return?WebContextFactory.get().getSession();??
  115. ????}??
  116. ??
  117. }??



RemoteMessageClient? 客户端?

Java代码??收藏代码
  1. package?com.gw.medical.hospital.utils.dwr;??
  2. ??
  3. import?java.util.Collection;??
  4. import?java.util.Iterator;??
  5. ??
  6. import?javax.servlet.ServletException;??
  7. import?javax.servlet.http.HttpSession;??
  8. ??
  9. import?org.directwebremoting.Browser;??
  10. import?org.directwebremoting.ScriptBuffer;??
  11. import?org.directwebremoting.ScriptSession;??
  12. import?org.directwebremoting.ScriptSessionFilter;??
  13. import?org.directwebremoting.WebContextFactory;??
  14. import?org.directwebremoting.annotations.RemoteMethod;??
  15. import?org.directwebremoting.annotations.RemoteProxy;??
  16. ??
  17. @RemoteProxy(name?=?"RemoteMessageClient")??
  18. public?class?RemoteMessageClient?{??
  19. ??
  20. ????/**?
  21. ?????*?客户端初始化?
  22. ?????*/??
  23. ????@RemoteMethod??
  24. ????public?void?onPageLoadClient()?{??
  25. ????????ScriptSession?scriptSession?=?getScriptSession();??
  26. ????????HttpSession?httpSession?=?getHttpSession();??
  27. ????????String?clientName?=?scriptSession.getId();//?获取seciptSessionID作为clientName??
  28. ????????httpSession.setAttribute("clientName",?clientName);??
  29. ????????RemoteMessageConstant.getMapList().put(clientName,?scriptSession);//?存储用户信息到在线用户列表??
  30. ????????RemoteMessageConstant.getUserNamList().put(clientName,?"游客");??
  31. ????????//?服务器显示在线用户列表;??
  32. ????????showOnlinClient();??
  33. ????????DwrScriptSessionManagerUtil?dssm?=?new?DwrScriptSessionManagerUtil();??
  34. ????????try?{??
  35. ????????????dssm.init();??
  36. ????????}?catch?(ServletException?e)?{??
  37. ????????????e.printStackTrace();??
  38. ????????}??
  39. ????}??
  40. ??
  41. ????/**?
  42. ?????*?客户端发送信息?
  43. ?????*??
  44. ?????*?@param?message?
  45. ?????*/??
  46. ????@RemoteMethod??
  47. ????public?void?addMessageClient(String?message)?{??
  48. ????????HttpSession?httpSession?=?getHttpSession();??
  49. ????????String?clientName?=?httpSession.getAttribute("clientName")?==?null???""?:?httpSession.getAttribute("clientName").toString();//?获取当前登录客户端ID,在用户列表中查找相应客服??
  50. ????????String?autoMessage?=?message;//?发送的内容??
  51. ????????ScriptSession?scriptSession?=?RemoteMessageConstant.getMappingList().get(clientName);//?从对应Map中获取,当前客户端对应的服务端ScriptSession??
  52. ??
  53. ????????if?(scriptSession?!=?null)?{??
  54. ????????????String?clientMessage=RemoteMessageConstant.getUserNamList().get(clientName);??
  55. ????????????clientMessage+=":"+autoMessage;??
  56. ????????????sendMessage(scriptSession,"receiveMessages",clientMessage);??
  57. ????????}??
  58. ????}??
  59. ??
  60. ????/**?
  61. ?????*?更改服务端在线用户列表?
  62. ?????*/??
  63. ????public?void?showOnlinClient()?{??
  64. ????????Browser.withAllSessionsFiltered(new?ScriptSessionFilter()?{??
  65. ????????????//?筛选scriptSession的用户,发送目标ID,是否在scriptSession中存在??
  66. ????????????public?boolean?match(ScriptSession?session)?{??
  67. ????????????????if?(RemoteMessageConstant.getServerList().get(session.getId())?!=?null)?{??
  68. ????????????????????return?true;??
  69. ????????????????}?else?{??
  70. ????????????????????return?false;??
  71. ????????????????}??
  72. ????????????}??
  73. ????????},?new?Runnable()?{??
  74. ????????????private?ScriptBuffer?script?=?new?ScriptBuffer();??
  75. ??
  76. ????????????public?void?run()?{??
  77. ????????????????//?输出内容到页面??
  78. ????????????????Iterator<String>?it?=?RemoteMessageConstant.getMapList().keySet().iterator();??
  79. ????????????????String?autoMessage?=?new?String();??
  80. ????????????????while?(it.hasNext())?{??
  81. ????????????????????String?key?=?it.next().toString();??
  82. ????????????????????//?如果没有被分配客服,显示在等待列表中??
  83. ????????????????????if?(RemoteMessageConstant.getMappingList().get(key)?==?null)?{??
  84. ??
  85. ????????????????????????autoMessage?+=?RemoteMessageConstant.getUserNamList().get(key)?+?";";??
  86. ????????????????????}??
  87. ????????????????}??
  88. ????????????????script.appendCall("showOnlinClient",?autoMessage);??
  89. ????????????????Collection<ScriptSession>?sessions?=?Browser.getTargetSessions();??
  90. ????????????????for?(ScriptSession?scriptSession?:?sessions)?{??
  91. ????????????????????scriptSession.addScript(script);??
  92. ????????????????}??
  93. ????????????}??
  94. ????????});??
  95. ????}??
  96. ??
  97. ??
  98. ????/**?
  99. ?????*?get?set?
  100. ?????*/??
  101. ????/**?
  102. ?????*?调用页面JS?
  103. ?????*??
  104. ?????*?@param?scriptSession?
  105. ?????*????????????,jsName:JS方法名,message:发送的信息?
  106. ?????*/??
  107. ????public?void?sendMessage(ScriptSession?scriptSession,?String?jsName,?String?message)?{??
  108. ????????ScriptBuffer?script?=?new?ScriptBuffer();??
  109. ????????script.appendCall(jsName,?message);??
  110. ????????scriptSession.addScript(script);??
  111. ????}??
  112. ??
  113. ????public?ScriptSession?getScriptSession()?{??
  114. ??
  115. ????????return?WebContextFactory.get().getScriptSession();??
  116. ????}??
  117. ??
  118. ????public?HttpSession?getHttpSession()?{??
  119. ??
  120. ????????return?WebContextFactory.get().getSession();??
  121. ????}??
  122. }??




DwrScriptSessionManagerUtil 监听?

Java代码??收藏代码
  1. package?com.gw.medical.hospital.utils.dwr;??
  2. ??
  3. import?javax.servlet.ServletException;??
  4. ??
  5. import?org.directwebremoting.Container;??
  6. import?org.directwebremoting.ScriptBuffer;??
  7. import?org.directwebremoting.ScriptSession;??
  8. import?org.directwebremoting.ServerContextFactory;??
  9. import?org.directwebremoting.event.ScriptSessionEvent;??
  10. import?org.directwebremoting.event.ScriptSessionListener;??
  11. import?org.directwebremoting.extend.ScriptSessionManager;??
  12. import?org.directwebremoting.servlet.DwrServlet;??
  13. ??
  14. public?class?DwrScriptSessionManagerUtil?extends?DwrServlet?{??
  15. ??
  16. ????private?static?final?long?serialVersionUID?=?-7504612622407420071L;??
  17. ??
  18. ????public?void?init()?throws?ServletException?{??
  19. ????????Container?container?=?ServerContextFactory.get().getContainer();??
  20. ????????ScriptSessionManager?manager?=?container.getBean(ScriptSessionManager.class);??
  21. ????????ScriptSessionListener?listener?=?new?ScriptSessionListener()?{??
  22. ????????????public?void?sessionCreated(ScriptSessionEvent?ev)?{??
  23. ??
  24. ????????????}??
  25. ??
  26. ????????????public?void?sessionDestroyed(ScriptSessionEvent?ev)?{??
  27. ????????????????if?(RemoteMessageConstant.getMappingList().get(ev.getSession().getId())?!=?null)?{??
  28. ????????????????????ScriptSession?scriptSession?=?RemoteMessageConstant.getMappingList().get(ev.getSession().getId());??
  29. ????????????????????sendMessage(scriptSession,"receiveMessages","客户端已关闭!");??
  30. ????????????????}??
  31. ????????????????//?删除该用户的列表信息??
  32. ????????????????RemoteMessageConstant.getMapList().remove(ev.getSession().getId());??
  33. ????????????????RemoteMessageConstant.getMappingList().remove(ev.getSession().getId());??
  34. ????????????????RemoteMessageConstant.getServerList().remove(ev.getSession().getId());??
  35. ????????????????RemoteMessageConstant.getUserNamList().remove(ev.getSession().getId());??
  36. ????????????????//?更新服务端在线列表??
  37. ????????????????RemoteMessageClient?rc?=?new?RemoteMessageClient();??
  38. ????????????????rc.showOnlinClient();??
  39. ????????????}??
  40. ????????};??
  41. ????????manager.addScriptSessionListener(listener);??
  42. ????}??
  43. ????/**?
  44. ?????*?调用页面JS?
  45. ?????*?@param?scriptSession,jsName:JS方法名,message:发送的信息?
  46. ?????*/??
  47. ????public?void?sendMessage(ScriptSession?scriptSession,?String?jsName,?String?message)?{??
  48. ????????ScriptBuffer?script?=?new?ScriptBuffer();??
  49. ????????script.appendCall(jsName,?message);??
  50. ????????scriptSession.addScript(script);??
  51. ????}??
  52. }??



RemoteMessageConstant 静态常量?

Java代码??收藏代码
  1. package?com.gw.medical.hospital.utils.dwr;??
  2. ??
  3. import?java.util.HashMap;??
  4. import?java.util.Map;??
  5. ??
  6. import?org.directwebremoting.ScriptSession;??
  7. ??
  8. public?class?RemoteMessageConstant?{??
  9. ????private?static?Map<String,?ScriptSession>?mapList?=?new?HashMap<String,?ScriptSession>();//?在线用户,Key为在线用户,Value为对应的客服,如果为空表示当前无客服接入;用户等待列表??
  10. ????private?static?Map<String,?String>?userNamList?=?new?HashMap<String,?String>();//?在线用户名称??
  11. ????private?static?Map<String,?ScriptSession>?mappingList?=?new?HashMap<String,?ScriptSession>();//?对应关系Map??
  12. ????private?static?Map<String,?ScriptSession>?serverList?=?new?HashMap<String,?ScriptSession>();//?服务端在线列表??
  13. ??
  14. ????/**?
  15. ?????*?get?set?
  16. ?????*/??
  17. ????public?static?Map<String,?ScriptSession>?getMapList()?{??
  18. ????????return?mapList;??
  19. ????}??
  20. ??
  21. ????public?static?void?setMapList(Map<String,?ScriptSession>?mapList)?{??
  22. ????????RemoteMessageConstant.mapList?=?mapList;??
  23. ????}??
  24. ??
  25. ????public?static?Map<String,?ScriptSession>?getMappingList()?{??
  26. ????????return?mappingList;??
  27. ????}??
  28. ??
  29. ????public?static?void?setMappingList(Map<String,?ScriptSession>?mappingList)?{??
  30. ????????RemoteMessageConstant.mappingList?=?mappingList;??
  31. ????}??
  32. ??
  33. ????public?static?Map<String,?ScriptSession>?getServerList()?{??
  34. ????????return?serverList;??
  35. ????}??
  36. ??
  37. ????public?static?void?setServerList(Map<String,?ScriptSession>?serverList)?{??
  38. ????????RemoteMessageConstant.serverList?=?serverList;??
  39. ????}??
  40. ??
  41. ????public?static?Map<String,?String>?getUserNamList()?{??
  42. ????????return?userNamList;??
  43. ????}??
  44. ??
  45. ????public?static?void?setUserNamList(Map<String,?String>?userNamList)?{??
  46. ????????RemoteMessageConstant.userNamList?=?userNamList;??
  47. ????}??
  48. ??
  49. ??
  50. }??



serverMessage.jsp? 客服页?

Java代码??收藏代码
  1. <%@?page?language="java"?contentType="text/html;?charset=utf-8"??pageEncoding="utf-8"%>??
  2. <!DOCTYPE?html?>??
  3. <html>??
  4. <head>??
  5. <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">??
  6. <title>Server</title>??
  7. <link?rel="stylesheet"?type="text/css"?href="<%=request.getContextPath()%>/css/themes/default/easyui.css">??
  8. <script?type="text/javascript"?src="<%=request.getContextPath()%>/dwr/engine.js">?</script>??
  9. <script?type='text/javascript'?src='<%=request.getContextPath()%>/dwr/util.js'></script>??
  10. <script?type="text/javascript"?src="<%=request.getContextPath()%>/dwr/interface/RemoteMessageServer.js">?</script>??
  11. <script?type="text/javascript"?src="<%=request.getContextPath()%>/js/jquery-1.8.3.min.js">?</script>??
  12. <script?type="text/javascript"?src="<%=request.getContextPath()%>/js/jquery.easyui.min.js">?</script>??
  13. <script?type="text/javascript">??
  14. ????$(function(){??
  15. ????????dwr.engine.setActiveReverseAjax(true);??
  16. ????????dwr.engine.setNotifyServerOnPageUnload(true);??
  17. ????????onPageLoad();??
  18. ????????//对话框初始化??
  19. ????????$("#messageDialog").dialog({??
  20. ????????????title:'对话框',??
  21. ????????????width:600,??
  22. ????????????height:550,??
  23. ????????????closed:false,??
  24. ????????????cache:false??
  25. ????????});??
  26. ????});??
  27. ??????function?sendMessage(message)?{??
  28. ????????????//?通过代理调用后台的addMessage方法发送消息??
  29. ????????????RemoteMessageServer.addMessage(message);??
  30. ????}??
  31. ????//?前台接受消息的方法,由后台调用???
  32. ??????function?receiveMessages(messages)?{??
  33. ????????????var?p=$("??
  34. ");??
  35. ????????????$(p).append(messages);??
  36. ????????????$("#messageContent").append(p);??
  37. ??????}??
  38. ????????
  39. ??????//读取name值作为推送的唯一标示??
  40. ??????function?onPageLoad(){??
  41. ????????//?通过代理,传入区别本页面的唯一标识符??
  42. ????????RemoteMessageServer.onPageLoad();??
  43. ???????}??
  44. ???????function?selectClient(){??
  45. ???????????RemoteMessageServer.selectClient({??
  46. ???????????????callback:function(clientName)?{???
  47. ??????????????????if(clientName=="")??
  48. ??????????????????????alert("选择失败!")??
  49. ??????????????????else??
  50. ?????????????????????$("#messageContent").append("客户:"+clientName+",已成功接入!"+"<br/>");??
  51. ?????????????????????$("#messageDialog").dialog({title:'客户:'+clientName});??
  52. ???????????????}??
  53. ?????????????????
  54. ???????????});??
  55. ???????}??
  56. ???????function?showOnlinClient(str){??
  57. ???????????$(".onlineUser").html(str);??
  58. ???????}??
  59. ????????/**?
  60. ?????*?页面JS?
  61. ?????*/??
  62. ?????function?showIcon(){??
  63. ????????if($("#icondiv").css("display")=="none"){??
  64. ????????????$("#icondiv").css("display","block");??
  65. ????????}else{??
  66. ????????????$("#icondiv").css("display","none");??
  67. ????????}??
  68. ?????}??
  69. ?????function?addIcon(str){??
  70. ?????????var?img=$('<img?src="<%=request.getContextPath()%>/images/dwr/'+str+'.gif"/>');??
  71. ?????????$("#messageTextArea").append($(img));??
  72. ?????}??
  73. ?????function?jsSendMessage(){??
  74. ?????????var?message=$("#messageTextArea").html();??
  75. ?????????$("#messageContent").append("我:"+message+"<br/>");??
  76. ?????????$("#messageTextArea").html("");??
  77. ?????????this.sendMessage(message);??
  78. ?????}??
  79. ??????????
  80. </script>??
  81. <style?type="text/css">??
  82. ????#icondiv?ul{??
  83. ????????width:?400px;???
  84. ????????height:?130px;??
  85. ????????overflow:?auto;??
  86. ????????border:?1px?solid?black;??
  87. ????}??
  88. ????#icondiv?ul?li{??
  89. ????????float:?left;??
  90. ????????width:?45px;??
  91. ????????list-style:?none;??
  92. ??????????
  93. ????}??
  94. ????#icondiv?ul?li?img{??
  95. ????????width:?40px;??
  96. ????????height:?40px;??
  97. ????}??
  98. </style>??
  99. </head>??
  100. <body>??
  101. ????[align=center;]??
  102. ????????????[url=javascript:void(0)]发送[/url]??
  103. ????????????[/align]??
  104. ????????</div>??
  105. ????</div>??
  106. ????<div><input?type="button"?onclick="selectClient()"?value="selectClient"/></div>??
  107. ????[align=center;]??
  108. ????????????[url=javascript:void(0)]发送[/url]??
  109. ????????????[/align]??
  110. ????????</div>??
  111. ????</div>??
  112. </body>??
  113. </html> ?
发表评论
用户名: 匿名