- 浏览: 38508 次
- 性别:
- 来自: 北京
最新评论
-
fengzhaoyang:
serverMessage.jsp 代码不全吧客户端的jsp页 ...
基于DWR的点对点聊天实现 server---client
RemoteMessageServer 客服类
RemoteMessageClient 客户端
DwrScriptSessionManagerUtil 监听
RemoteMessageConstant 静态常量
serverMessage.jsp 客服页
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"); // 测试用,存入模拟userInfo信息 RemoteMessageConstant.getServerList().put(getScriptSession().getId(), getScriptSession());// 存储当前客服,到在线客服列表 } /** * 服务端发送信息 * * @param message */ @RemoteMethod public void addMessage(final String message) { HttpSession httpsession = getHttpSession();// 获取session String clientName = httpsession.getAttribute("clientName") == null ? "" : httpsession.getAttribute("clientName").toString();// 当前客户端ID ScriptSession session = RemoteMessageConstant.getMapList().get(clientName);// 根据clientName获取scriptSession; if (session != null) { // 发送消息 String serverMessage=getHttpSession().getAttribute("userInfo").toString(); serverMessage=serverMessage+":"+message; sendMessage(session,"receiveMessages",serverMessage); } } /** * 选择下一个客户端 * * @return boolean */ @RemoteMethod public String selectClient() { addMessage("本次会话结束!感谢您的咨询!"); // 销毁上一位客户 removeClient(); ScriptSession sessionServer = getScriptSession();// 获取客服ScriptSsession HttpSession httpsession = getHttpSession();// 获取session Map<String, ScriptSession> userMap = RemoteMessageConstant.getMapList();// 用户在线MAP Iterator<String> iterator = userMap.keySet().iterator(); String clientName = "";// 获取客户端列表中的,clientName; while (iterator.hasNext()) { // 判断当前用户是否有客服接入,如未接入,接入该客户 String tempName = iterator.next(); if (RemoteMessageConstant.getMappingList().get(tempName) == null) { clientName = tempName; break; } } ScriptSession session = userMap.get(clientName);// 根据clientName获取scriptSession; // 更改客户端的服务ID if (session != null) { // 调用页面JS sendMessage(session, "setServerName", httpsession.getAttribute("userInfo").toString()); httpsession.setAttribute("clientName", clientName);// 存储当前客户ID RemoteMessageConstant.getMappingList().put(session.getId(), sessionServer);// 存储对应关系,到关系MAP return RemoteMessageConstant.getUserNamList().get(clientName); } return ""; } /** * 销毁当前,服务端对应的客户端信息 */ public void removeClient() { HttpSession httpSession = getHttpSession();// 获取httpsession String clientName = httpSession.getAttribute("clientName") == null ? "" : httpSession.getAttribute("clientName").toString(); RemoteMessageConstant.getMappingList().remove(clientName);// 从关系Map中,删除对应关系。 RemoteMessageConstant.getMapList().remove(clientName);// 从用户Map中,删除该用户; RemoteMessageConstant.getUserNamList().remove(clientName);//从用户名列表 删除该用户名 httpSession.removeAttribute("clientName");// 删除当前服务端的,客户ID } /** * 调用页面JS * @param scriptSession,jsName:JS方法名,message:发送的信息 */ public void sendMessage(ScriptSession scriptSession, String jsName, String message) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(jsName, message); scriptSession.addScript(script); } /** * get set */ public ScriptSession getScriptSession() { return WebContextFactory.get().getScriptSession(); } public HttpSession getHttpSession() { return WebContextFactory.get().getSession(); } }
RemoteMessageClient 客户端
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();// 获取seciptSessionID作为clientName 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(); } } /** * 客户端发送信息 * * @param message */ @RemoteMethod public void addMessageClient(String message) { HttpSession httpSession = getHttpSession(); String clientName = httpSession.getAttribute("clientName") == null ? "" : httpSession.getAttribute("clientName").toString();// 获取当前登录客户端ID,在用户列表中查找相应客服 String autoMessage = message;// 发送的内容 ScriptSession scriptSession = RemoteMessageConstant.getMappingList().get(clientName);// 从对应Map中获取,当前客户端对应的服务端ScriptSession if (scriptSession != null) { String clientMessage=RemoteMessageConstant.getUserNamList().get(clientName); clientMessage+=":"+autoMessage; sendMessage(scriptSession,"receiveMessages",clientMessage); } } /** * 更改服务端在线用户列表 */ public void showOnlinClient() { Browser.withAllSessionsFiltered(new ScriptSessionFilter() { // 筛选scriptSession的用户,发送目标ID,是否在scriptSession中存在 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); } } }); } /** * get set */ /** * 调用页面JS * * @param scriptSession * ,jsName:JS方法名,message:发送的信息 */ 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 监听
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); } /** * 调用页面JS * @param scriptSession,jsName:JS方法名,message:发送的信息 */ public void sendMessage(ScriptSession scriptSession, String jsName, String message) { ScriptBuffer script = new ScriptBuffer(); script.appendCall(jsName, message); scriptSession.addScript(script); } }
RemoteMessageConstant 静态常量
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>();// 在线用户,Key为在线用户,Value为对应的客服,如果为空表示当前无客服接入;用户等待列表 private static Map<String, String> userNamList = new HashMap<String, String>();// 在线用户名称 private static Map<String, ScriptSession> mappingList = new HashMap<String, ScriptSession>();// 对应关系Map private static Map<String, ScriptSession> serverList = new HashMap<String, ScriptSession>();// 服务端在线列表 /** * get set */ 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 客服页
<%@ 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) { // 通过代理调用后台的addMessage方法发送消息 RemoteMessageServer.addMessage(message); } // 前台接受消息的方法,由后台调用 function receiveMessages(messages) { var p=$(" "); $(p).append(messages); $("#messageContent").append(p); } //读取name值作为推送的唯一标示 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); } /** * 页面JS */ 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>
发表评论
-
阿里巴巴JAVA开发手册
2017-02-13 13:48 544目录 一 、编程规约 编程规约 ............... ... -
druid 数据库密码加密
2017-01-23 14:59 374cmd命令: 1、切换的druid-0.2.9.jar包所在目 ... -
spring mvc 表单提交controller中文乱码
2015-03-02 16:11 1899用spring的编码器过滤所有请求,但还是有问题,最后发现是表 ... -
Hibernate hql left join
2015-01-30 17:13 10941.配置映射关系 2.from A表 left join A表 ... -
ajax 提交表单数据 到action
2015-01-27 15:11 465$.ajax({ url:'showDe ... -
easyui
2015-01-21 17:43 766$(function(){ var pageCou ... -
Jquery ImageMapste Map area
2015-01-09 09:29 720ImageMapste 可以给Map Area 添加背景色 ... -
Flex Repeater
2014-02-20 18:35 425<mx:VBox x="674" ... -
Flex Struts2 fileupload上传文件
2014-02-20 18:33 723file=new FileReference(); ... -
Myeclipse提示Jquery
2011-08-31 11:22 924项目右键properties MyEclipse -> ... -
java获取土豆视频
2011-04-02 14:03 1056通过JAVA获取土豆视频,现在很多社会网站都有这个功能,用户输 ... -
flex鱼眼菜单
2011-03-31 10:46 1753flex鱼眼菜单 -
datagrid 套combobox
2011-03-29 15:20 828<mx:DataGridColumn headerTex ... -
Web开发人员必备的20款超赞的jQuery插件
2011-02-24 08:51 721中文:http://sd.csdn.net/a/2011021 ... -
Oracle修改默认数据库,及启动关闭命令,查询连接的当前数据库
2011-02-22 09:55 2724Oracle Windows平台修改默认sid假如window ... -
JQuery的thickbox插件,弹出窗
2011-02-17 11:29 818http://jquery.com/demo/thickbox ... -
JQuery的jqzoom插件的两种使用方法
2011-02-17 09:26 5199第一种实现的方法 注意:本方法用的是jqzoom.pack.1 ... -
jQuery的appendto()移动
2011-02-16 09:45 1262var $self = obj.find("u ... -
flex 饼图缓动
2011-02-09 10:32 1224<?xml version="1.0&qu ... -
jquery图片动态加载示例
2011-01-25 09:39 2169<!DOCTYPE html PUBLIC &quo ...
相关推荐
基于dwr推送技术,结合jsp技术,webservice天气接口,以及发送表情技术的web聊天系统,识别电脑和手机设备,不足地方请指教,谢谢
基于dwr推送技术的聊天室,下载后解压,可直接运行
spring3 dwr3 聊天 dwr server push spring3+dwr3实现聊天功能 使用DWR SERVER PUSH 实现聊天功能
基于DWR的实时web聊天系统 dwr基本技术实现
DWR框架推模式实现的聊天室DWR框架推模式实现的聊天室DWR框架推模式实现的聊天室
java+dwr框架实现聊天室,
基于DWR的AJAX技术研究与实现.pdf
使用dwr框架的服务器推技术也称为反向ajax或comet,将项目发布到ajax中输入http://localhost:8080/dwr即可聊天 我实现的时群聊
基于DWR的AJAX技术研究与实现 (1).pdf
DWR在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能 java jsp页面
开发平台myeclipse,mysql,tomcat。前端Extjs 使用Extjs开发的一个在线聊天工具,用DWR反推实现!
DWR 在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能.rarDWR 在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能.rarDWR 在线即时聊天系统,实现了对指定用户发送消息,和即时显示功能.rarDWR 在线...
基于DWR的webIM系统,利用反向ajax(comet)技术和dwr框架实现了聊天室和点对点聊天的功能,项目运行起来后打开页面,输入用户名即可登录,登录后用户会显示在左侧用户框中,若想与用户私聊,在用户框中点击用户,在...
包含了dwr推送技术的详细介绍 和 一个基于dwr推送技术的聊天实例
DWR框架的实现DWR框架的实现DWR框架的实现DWR框架的实现DWR框架的实现
dwr-yui实现分页,dwr-yui实现分页,dwr-yui实现分页
dwr实现的聊天室,采用服务器控制客户端刷新,显示用户连接信息,是深入学习dwr的必看教程! 源码分为一个java文件和一个html文件,还有一个dwr配置文件,根据自己的路径进行修改。
基于DWR框架的Web推送技术实现的web聊天程序,可以从这个代码开始了解web推送技术以及dwr框架
dwr资源包,包含dwr.jar/dwr-2.0.5-src.zip/dwr.zip
基于DWR框架实现的服务器推工程 demo实现了dwr框架的配置,页面的展示,通过bean传参的功能 demo绝对可以跑起来