Não pode escolher mais do que 25 tópicos
Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
59 linhas
1.9 KiB
59 linhas
1.9 KiB
há 2 anos
|
package com.aiprose.im.config;
|
||
|
|
||
|
import com.aiprose.im.socket.MsgVo;
|
||
|
import com.aiprose.im.socket.WebSocketPool;
|
||
|
import com.aiprose.im.utils.RedisUtil;
|
||
|
import com.alibaba.fastjson.JSON;
|
||
|
import org.apache.commons.lang3.StringUtils;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.stereotype.Component;
|
||
|
|
||
|
import java.util.List;
|
||
|
|
||
|
/**
|
||
|
* @author yanpeng
|
||
|
* @version 1.0
|
||
|
* @desc TODO
|
||
|
* @company 北京中经网软件有限公司
|
||
|
* @date 2022/3/3 13:49
|
||
|
*/
|
||
|
@Component
|
||
|
public class RedisReceiver {
|
||
|
|
||
|
@Autowired
|
||
|
private WebSocketPool socketPool;
|
||
|
|
||
|
@Autowired
|
||
|
private RedisUtil redisUtil;
|
||
|
|
||
|
public void receiveMessage(String message){
|
||
|
MsgVo msgVo = JSON.parseObject(message, MsgVo.class);
|
||
|
try {
|
||
|
// 点对点发送消息
|
||
|
if (StringUtils.isNotBlank(msgVo.getReceiveUserId())) { // p2p
|
||
|
if(redisUtil.hasKeyHash(WebSocketPool.HASHKEY,msgVo.getReceiveUserId())){
|
||
|
String sessionid = redisUtil.getHashIndex(WebSocketPool.HASHKEY, msgVo.getReceiveUserId()).toString();
|
||
|
if(socketPool.has(sessionid)){
|
||
|
socketPool.get(sessionid).getAsyncRemote().sendText(message);
|
||
|
}
|
||
|
}
|
||
|
} else { // 群发
|
||
|
List<Object> sessions = redisUtil.getHash(WebSocketPool.HASHKEY);
|
||
|
for (Object item : sessions) {
|
||
|
//如果某个session不存在,分布式环境下可能session在其他的节点,继续给其他人广播
|
||
|
try {
|
||
|
if(socketPool.has(item.toString())){
|
||
|
socketPool.get(item.toString()).getAsyncRemote().sendText(message);
|
||
|
}
|
||
|
}catch (Exception e1){
|
||
|
e1.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
} catch (Exception e) {
|
||
|
e.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|