前言:小刘整合并使用SpringBoot+socket.io+netty 实现及时通讯系统
难点:1.socket.io的消息推送
2.集群下netty网络通讯
3.私聊群聊的逻辑实现
5.高并发的性能提高
简图:
视频:
核心代码:
socket.on('SINGLE_MSG', function(data) {
// console.log("收到服务端发送的消息")
console.log(userId+"服务器发来私聊消息"+JSON.stringify(data))
//私聊
let p="<p>"+JSON.stringify(data)+"</p>";
$("#cfL").append(p)
});
后端私聊和群发
/**群发
* 广播消息
*/
public static void sendBroadMsg(List<RespMsg>respMsgList) {
log.info("=sendBroadMsg==websocket发起响应===");
respMsgList.stream().forEach(t->{
log.info(JSONObject.toJSONString(t));
if (socketIOClientMap.containsKey(t.getReceiveId())){
log.info("====sendBroadMsg===websocket---receiveId========"+t.getReceiveId());
SocketIOClient client = socketIOClientMap.get(t.getReceiveId());
if (client.isChannelOpen()){
client.sendEvent(GROUP_MSG, t, System.currentTimeMillis());
}
}
});
}
/**
* 私发
* @param
*/
public static void sendSingleMsg(RespMsg resp){
SocketIOClient acceptClient = socketIOClientMap.get(resp.getReceiveId());
if (acceptClient!=null&&acceptClient.isChannelOpen()){
log.info(resp.getSendId()+"发送消息sendSingleMsg给receiveId"+resp.getReceiveId());
acceptClient.sendEvent(SINGLE_MSG,resp, System.currentTimeMillis());
}else {
log.info("sendSingleMsg本系统接收方不在线!");
}
SocketIOClient sendClient = socketIOClientMap.get(resp.getSendId());
if (sendClient!=null&&sendClient.isChannelOpen()){
log.info(resp.getSendId()+"发送消息给自身"+resp.getSendId());
sendClient.sendEvent(SINGLE_MSG, resp, System.currentTimeMillis());
}else {
log.info("本系统发送方不在线!");
}
}
无偿免费分享源码以及技术和面试文档,更多优秀精致的源码技术栈分享请关注微信公众号:gh_817962068649