此次是在第一步通信上面加以補充 離線消息用的redis 作消息隊列,假設 Apache mina作推送 http端請求發送信息 所有是json格式統一
java
查詢用戶 public NewsObject findByName(NewsObject o) throws Exception{ // TODO Auto-generated method stub Json json = new Json(); logger.info("com.xinhua.ServiceImpl.UserServiceImpl.findByName into"); if (((UserForm) o).getUsername() == null || ((UserForm) o).getUsername().equals("")) { json.setReturnNo(USER_NULL); logger.warn("login error code:"+USER_NULL); return json; } else if (((UserForm) o).getPassword() == null || ((UserForm) o).getPassword().trim().equals("")) { json.setReturnNo(PASSWORD_NULL); logger.warn("login error code:"+PASSWORD_NULL); return json; } ((UserForm) o).setUsername(((UserForm) o).getUsername().trim()); Users user = (Users) usersMapper.findByName(o); if (user == null || !user.getUsername().equals(((UserForm) o).getUsername().trim()) || !user.getPassword().equals(((UserForm) o).getPassword())) { json.setReturnNo(USER_PASSWROD_ERROR); logger.warn("login error code:"+USER_PASSWROD_ERROR); return json; } json.setReturnNo(SESSION_CODE); logger.info("User Login Success"); json.setRow(user); logger.info("com.xinhua.ServiceImpl.UserServiceImpl.findByName into"); return json; } 返回app 接口代碼 public NewsObject loginApp(UserForm form) throws Exception { // TODO Auto-generated method stub Json json = (Json) findByName(form); final String userid = ((Users)json.getRow()).getId(); //String url = ""; if((SESSION_CODE)==json.getReturnNo()){//若是等於session code 那麼表示登陸成功 form.setLoginKey(Resource.getInstall().randNumber()); json.setMsg(form.getLoginKey()); updateLoginKey(form); //讀取全部離線消息 List<Object> list = redisTemplate.opsForList().range(REDIS_MESSAGE+((Users)json.getRow()).getId(),0,redisTemplate.opsForList().size(REDIS_MESSAGE+((Users)json.getRow()).getId())); for (Object object : list) { System.out.println("list -------"+object); } ((Users)json.getRow()).setMsgList(list); //讀取redis 消息隊列的消息 進行返回用戶界面 //request.getRequestDispatcher("/user/web/index.do"); //url = "redirect:/user/web/index.do";//進行跳轉操做 } return json; } 短連接發送消息代碼 進行發送消息 public void sendMsg(NewsObject object) { MessageForm form = (MessageForm)object; UserForm user = new UserForm(); user.setId((form.getReviceId())); Users users = ((Users)userService.findByIdGetUser(user)); if(users.getLoginStatus()==1){//若是在線表示Apache mina 在線 進行在線消息推送 Message message = new Message(); message.setType(5);//web版本短請求發送消息 user.setId(String.valueOf(form.getSendId())); message.setUsername(((Users)userService.findByIdGetUser(user)).getUsername()); message.setMsg(form.getMsg()); message.setId(form.getId()); MyIoHandleWeb.getSession().write(JSON.toJSONString(message)); } messageMapper.insertMessage(form); redisTemplate.opsForList().rightPush(REDIS_MESSAGE+form.getReviceId(), JSON.toJSONString(form)); } 服務端核心代碼 當接受消息是5表示推送消息 那麼進行查詢操做 進行消息推送 if(msg.getType()==5){ if(mina.map.get("user"+msg.getId()) != null){ mina.map.get("user"+msg.getId()).write(JSON.toJSONString(msg));//寫到客戶端 } }
今天的代碼更新到此 後續會作 一些新的功能
web