@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);
      }
    }
  }
Ejemplo n.º 2
0
 /**
  * 处理发送消息
  *
  * @param message
  */
 private static void sendMessage(InfoMessage message) {
   message.setStatus(MessageStatus.READED.ordinal());
   message.setReadTime(new Date());
   if (!MessageType.success.name().equals(message.getType())) {
     messageService.save(message);
   }
   // Loop over all the users on the current page
   Collection<ScriptSession> pages = loadScriptPages();
   Department msgDepartment = message.getDepartment();
   String msgDepartId = null;
   if (msgDepartment != null) {
     msgDepartId = msgDepartment.getId();
   }
   for (Iterator<ScriptSession> it = pages.iterator(); it.hasNext(); ) {
     ScriptSession otherSession = it.next();
     if (msgDepartId != null) {
       String sessionDepartId = (String) otherSession.getAttribute(DEPART_ID);
       if (!msgDepartId.equals(sessionDepartId)) {
         continue;
       }
     }
     Util util = new Util(otherSession);
     util.addFunctionCall("showAppMessage", message);
   }
 }
Ejemplo n.º 3
0
 /** 添加在线用户 */
 public void addOnlineUser() {
   WebContext context = WebContextFactory.get();
   String userId = AppLoginUtils.getLoginUserId();
   String userName = AppLoginUtils.getLoginUserName();
   String departmentId = AppLoginUtils.getDepartmentId();
   ScriptSession scriptSession = context.getScriptSession();
   scriptSession.setAttribute(USER_ID, userId);
   scriptSession.setAttribute(USER_NAME, userName);
   scriptSession.setAttribute(DEPART_ID, departmentId);
 }