case FriendPushNotice : {//手機端推送好友列表
log.debug("socket:msgtype=FriendPushNotice");
friendPushNoticeHandler.handleMsg(ctx, msgVo);
break;
}html
package com.jubotech.framework.netty.handler.socket;java
import java.util.List;web
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;spring
import com.google.protobuf.util.JsonFormat;
import com.jubotech.business.web.domain.AccountInfo;
import com.jubotech.business.web.domain.WeChatAccountInfo;
import com.jubotech.business.web.domain.WeChatContactInfo;
import com.jubotech.business.web.service.AccountService;
import com.jubotech.business.web.service.WeChatAccountService;
import com.jubotech.business.web.service.WeChatContactService;
import com.jubotech.framework.netty.common.Constant;
import com.jubotech.framework.netty.utils.MessageUtil;
import com.jubotech.framework.netty.utils.NettyConnectionUtil;數據庫
import Jubo.JuLiao.IM.Wx.Proto.FriendAddNotice.FriendMessage;
import Jubo.JuLiao.IM.Wx.Proto.FriendPushNotice.FriendPushNoticeMessage;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumMsgType;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import io.netty.channel.ChannelHandlerContext;微信
@Service
public class FriendPushNoticeHandler{
private final Logger log = LoggerFactory.getLogger(getClass());
@Autowired
private WeChatAccountService weChatAccountService;
@Autowired
private AccountService accountService;
@Autowired
private WeChatContactService weChatContactService;dom
/** * 手機端主動推送好友列表消息 * @author wechatno:tangjinjinwx * @param ctx * @param vo */ public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) { try { FriendPushNoticeMessage req = vo.getContent().unpack(FriendPushNoticeMessage.class); log.info(JsonFormat.printer().print(req)); // 把消息轉發給pc端 // a、根據wechatId找到accountid // b、經過accountid找到account // c、經過account帳號找到通道 WeChatAccountInfo account = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId()); if (null != account && null != account.getAccountid() && 1 != account.getIslogined()) { AccountInfo accInfo = accountService.findAccountInfoByid(account.getAccountid()); if (null != accInfo) { // 轉發給pc端 ChannelHandlerContext chx = NettyConnectionUtil.getClientChannelHandlerContextByUserId(accInfo.getAccount()); if (null != chx) { asyncSendMsg(chx, req); } } // 告訴客戶端消息已收到 MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null); //異步保存到數據庫 friendListSave(req, account); } else { // 對方不在線 MessageUtil.sendErrMsg(ctx, EnumErrorCode.TargetNotOnline, Constant.ERROR_MSG_NOTONLINE); } } catch (Exception e) { e.printStackTrace(); MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL); } } @Async private void asyncSendMsg(ChannelHandlerContext chx,FriendPushNoticeMessage req){ MessageUtil.sendJsonMsg(chx, EnumMsgType.FriendPushNotice, NettyConnectionUtil.getNettyId(chx),null, req); } @Async private void friendListSave(FriendPushNoticeMessage req,WeChatAccountInfo account) { List<FriendMessage> friendList = req.getFriendsList(); if(null != friendList && friendList.size()>0){ String wechatId = req.getWeChatId(); for(int i=0;i<friendList.size();i++){ FriendMessage friend = friendList.get(i); if(null != friend){ WeChatContactInfo contactinfo = weChatContactService.findContactinfoByfriendid(account.getCid(),wechatId,friend.getFriendId()); try { if(null != contactinfo){ setContActinfo(contactinfo, friend); weChatContactService.update(contactinfo); }else{ contactinfo = new WeChatContactInfo(); setContActinfo(contactinfo, friend); contactinfo.setCid(account.getCid()); contactinfo.setWechatid(req.getWeChatId()); contactinfo.setFriendid(friend.getFriendId()); weChatContactService.insert(contactinfo); } } catch (Exception e) { e.printStackTrace(); } } } } } private static void setContActinfo(WeChatContactInfo contactinfo,FriendMessage friend){ contactinfo.setFriend_wechatno(friend.getFriendNo()); contactinfo.setNickname(friend.getFriendNick()); contactinfo.setGender(friend.getGenderValue()); contactinfo.setAvatar(friend.getAvatar()); contactinfo.setCountry(friend.getCountry()); contactinfo.setProvince(friend.getProvince()); contactinfo.setCity(friend.getCity()); contactinfo.setState(0); }
}異步
項目地址:https://www.wuliaokankan.cn/url301/138.html
接口參考:http://www.yunlauncher.com/Blog/articles/119.htmlsocket