@SuppressWarnings("deprecation") public void onApplicationEvent(ApplicationEvent event) { // 如果事件类型是ChatMessageEvent就执行下面操作 if (event instanceof ChatMessageEvent) { Message msg = (Message) event.getSource(); ServerContext context = ServerContextFactory.get(); // 获得客户端所有chat页面script session连接数 Collection<ScriptSession> sessions = context.getAllScriptSessions(); for (ScriptSession session : sessions) { ScriptBuffer sb = new ScriptBuffer(); Date time = msg.getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /*String s = time.getYear() + "-" + (time.getMonth() + 1) + "-" + time.getDate() + " " + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();*/ // 执行setMessage方法 String s = sdf.format(time); sb.appendScript("index.showMessage({msg: '") .appendScript(msg.getMsg()) .appendScript("', time: '") .appendScript(s) .appendScript("'})"); // 执行客户端script session方法,相当于浏览器执行JavaScript代码 // 上面就会执行客户端浏览器中的showMessage方法,并且传递一个对象过去 session.addScript(sb); } } }
/** * 供DWR框架调用获得Bean * * @param beanName * @return */ public static Object getBeanOfDwr(String beanName) { try { ServerContext ctx = ServerContextFactory.get(); if (ctx == null) { LOG.warn("ServerContext[ServerContextFactory.get()] is null."); return null; } Container container = ctx.getContainer(); return container.getBean(beanName); } catch (Exception e) { LOG.error(e.getMessage(), e); } return null; }