mirror of https://gitee.com/stylefeng/roses
1.修改类命名
parent
e6f887294d
commit
439eab3f21
|
@ -9,7 +9,7 @@ import cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession;
|
|||
import cn.stylefeng.roses.kernel.socket.business.websocket.message.SocketMessageCenter;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.pojo.WebSocketMessageDTO;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.session.SessionCenter;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GettySocketOperator;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GunsSocketOperator;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
@ -26,7 +26,7 @@ public class WebSocketOperator implements SocketOperatorApi {
|
|||
|
||||
@Override
|
||||
public void sendMsgOfUserSessionBySessionId(String msgType, String sessionId, Object msg) throws SocketException {
|
||||
SocketSession<GettySocketOperator> session = SessionCenter.getSessionBySessionId(sessionId);
|
||||
SocketSession<GunsSocketOperator> session = SessionCenter.getSessionBySessionId(sessionId);
|
||||
if (ObjectUtil.isEmpty(session)) {
|
||||
throw new SocketException(SocketExceptionEnum.SESSION_NOT_EXIST);
|
||||
}
|
||||
|
@ -39,14 +39,14 @@ public class WebSocketOperator implements SocketOperatorApi {
|
|||
@Override
|
||||
public void sendMsgOfUserSession(String msgType, String userId, Object msg) throws SocketException {
|
||||
// 根据用户ID获取会话
|
||||
List<SocketSession<GettySocketOperator>> socketSessionList = SessionCenter.getSessionByUserIdAndMsgType(userId);
|
||||
List<SocketSession<GunsSocketOperator>> socketSessionList = SessionCenter.getSessionByUserIdAndMsgType(userId);
|
||||
if (ObjectUtil.isEmpty(socketSessionList)) {
|
||||
throw new SocketException(SocketExceptionEnum.SESSION_NOT_EXIST);
|
||||
}
|
||||
WebSocketMessageDTO webSocketMessageDTO = new WebSocketMessageDTO();
|
||||
webSocketMessageDTO.setData(msg);
|
||||
webSocketMessageDTO.setServerMsgType(msgType);
|
||||
for (SocketSession<GettySocketOperator> session : socketSessionList) {
|
||||
for (SocketSession<GunsSocketOperator> session : socketSessionList) {
|
||||
// 发送内容
|
||||
session.getSocketOperatorApi().writeAndFlush(webSocketMessageDTO);
|
||||
}
|
||||
|
@ -54,12 +54,12 @@ public class WebSocketOperator implements SocketOperatorApi {
|
|||
|
||||
@Override
|
||||
public void sendMsgOfAllUserSession(String msgType, Object msg) {
|
||||
Collection<List<SocketSession<GettySocketOperator>>> values = SessionCenter.getSocketSessionMap().values();
|
||||
Collection<List<SocketSession<GunsSocketOperator>>> values = SessionCenter.getSocketSessionMap().values();
|
||||
WebSocketMessageDTO webSocketMessageDTO = new WebSocketMessageDTO();
|
||||
webSocketMessageDTO.setData(msg);
|
||||
webSocketMessageDTO.setServerMsgType(msgType);
|
||||
for (List<SocketSession<GettySocketOperator>> sessions : values) {
|
||||
for (SocketSession<GettySocketOperator> session : sessions) {
|
||||
for (List<SocketSession<GunsSocketOperator>> sessions : values) {
|
||||
for (SocketSession<GunsSocketOperator> session : sessions) {
|
||||
// 找到该类型的通道
|
||||
if (session.getMessageType().equals(msgType)) {
|
||||
session.getSocketOperatorApi().writeAndFlush(webSocketMessageDTO);
|
||||
|
|
|
@ -12,14 +12,14 @@ import java.io.IOException;
|
|||
* @author majianguo
|
||||
* @date 2021/6/1 下午3:41
|
||||
*/
|
||||
public class GettySocketOperator implements GettyChannelExpandInterFace {
|
||||
public class GunsSocketOperator implements SocketChannelExpandInterFace {
|
||||
|
||||
/**
|
||||
* 实际操作的通道
|
||||
*/
|
||||
private Session socketChannel;
|
||||
|
||||
public GettySocketOperator(Session socketChannel) {
|
||||
public GunsSocketOperator(Session socketChannel) {
|
||||
this.socketChannel = socketChannel;
|
||||
}
|
||||
|
|
@ -10,6 +10,6 @@ import cn.stylefeng.roses.kernel.socket.api.session.SocketSessionOperatorApi;
|
|||
* @author majianguo
|
||||
* @date 2021/6/1 下午3:44
|
||||
*/
|
||||
public interface GettyChannelExpandInterFace extends SocketSessionOperatorApi {
|
||||
public interface SocketChannelExpandInterFace extends SocketSessionOperatorApi {
|
||||
|
||||
}
|
|
@ -7,7 +7,7 @@ import cn.stylefeng.roses.kernel.socket.api.enums.ServerMessageTypeEnum;
|
|||
import cn.stylefeng.roses.kernel.socket.api.message.SocketMsgCallbackInterface;
|
||||
import cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.message.SocketMessageCenter;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GettySocketOperator;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GunsSocketOperator;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.pojo.WebSocketMessageDTO;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.session.SessionCenter;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
|
@ -41,7 +41,7 @@ public class WebSocketServer {
|
|||
@OnOpen
|
||||
public void onOpen(Session session, @PathParam("userId") String userId) {
|
||||
// 操作api包装
|
||||
GettySocketOperator gettySocketOperator = new GettySocketOperator(session);
|
||||
GunsSocketOperator GunsSocketOperator = new GunsSocketOperator(session);
|
||||
|
||||
// 回复消息
|
||||
WebSocketMessageDTO replyMsg = new WebSocketMessageDTO();
|
||||
|
@ -53,17 +53,17 @@ public class WebSocketServer {
|
|||
replyMsg.setData(session.getId());
|
||||
|
||||
// 创建会话对象
|
||||
SocketSession<GettySocketOperator> socketSession = new SocketSession<>();
|
||||
SocketSession<GunsSocketOperator> socketSession = new SocketSession<>();
|
||||
socketSession.setSessionId(session.getId());
|
||||
socketSession.setUserId(userId);
|
||||
socketSession.setSocketOperatorApi(gettySocketOperator);
|
||||
socketSession.setSocketOperatorApi(GunsSocketOperator);
|
||||
socketSession.setConnectionTime(System.currentTimeMillis());
|
||||
|
||||
// 维护会话
|
||||
SessionCenter.addSocketSession(socketSession);
|
||||
} finally {
|
||||
// 回复消息
|
||||
gettySocketOperator.writeAndFlush(replyMsg);
|
||||
GunsSocketOperator.writeAndFlush(replyMsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,7 +94,7 @@ public class WebSocketServer {
|
|||
WebSocketMessageDTO WebSocketMessageDTO = JSON.parseObject(message, WebSocketMessageDTO.class);
|
||||
|
||||
// 维护通道是否已初始化
|
||||
SocketSession<GettySocketOperator> socketSession = SessionCenter.getSessionBySessionId(socketChannel.getId());
|
||||
SocketSession<GunsSocketOperator> socketSession = SessionCenter.getSessionBySessionId(socketChannel.getId());
|
||||
|
||||
// 心跳包
|
||||
if (ObjectUtil.isNotEmpty(socketSession) && ClientMessageTypeEnum.USER_HEART.getCode().equals(WebSocketMessageDTO.getClientMsgType())) {
|
||||
|
|
|
@ -2,7 +2,7 @@ package cn.stylefeng.roses.kernel.socket.business.websocket.session;
|
|||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GettySocketOperator;
|
||||
import cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GunsSocketOperator;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
@ -21,16 +21,16 @@ public class SessionCenter {
|
|||
/**
|
||||
* 所有会话维护
|
||||
*/
|
||||
private static ConcurrentMap<String, List<SocketSession<GettySocketOperator>>> socketSessionMap = new ConcurrentHashMap<>();
|
||||
private static ConcurrentMap<String, List<SocketSession<GunsSocketOperator>>> socketSessionMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 获取维护的所有会话
|
||||
*
|
||||
* @return {@link ConcurrentMap< String, SocketSession<GettySocketOperator>>}
|
||||
* @return {@link ConcurrentMap< String, SocketSession<GunsSocketOperator>>}
|
||||
* @author majianguo
|
||||
* @date 2021/6/1 下午2:13
|
||||
**/
|
||||
public static ConcurrentMap<String, List<SocketSession<GettySocketOperator>>> getSocketSessionMap() {
|
||||
public static ConcurrentMap<String, List<SocketSession<GunsSocketOperator>>> getSocketSessionMap() {
|
||||
return socketSessionMap;
|
||||
}
|
||||
|
||||
|
@ -38,11 +38,11 @@ public class SessionCenter {
|
|||
* 根据用户ID获取会话信息列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return {@link SocketSession <GettySocketOperator>}
|
||||
* @return {@link SocketSession <GunsSocketOperator>}
|
||||
* @author majianguo
|
||||
* @date 2021/6/1 下午1:48
|
||||
**/
|
||||
public static List<SocketSession<GettySocketOperator>> getSessionByUserId(String userId) {
|
||||
public static List<SocketSession<GunsSocketOperator>> getSessionByUserId(String userId) {
|
||||
return socketSessionMap.get(userId);
|
||||
}
|
||||
|
||||
|
@ -50,11 +50,11 @@ public class SessionCenter {
|
|||
* 根据用户ID和消息类型获取会话信息列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return {@link SocketSession <GettySocketOperator>}
|
||||
* @return {@link SocketSession <GunsSocketOperator>}
|
||||
* @author majianguo
|
||||
* @date 2021/6/1 下午1:48
|
||||
**/
|
||||
public static List<SocketSession<GettySocketOperator>> getSessionByUserIdAndMsgType(String userId) {
|
||||
public static List<SocketSession<GunsSocketOperator>> getSessionByUserIdAndMsgType(String userId) {
|
||||
return socketSessionMap.get(userId);
|
||||
}
|
||||
|
||||
|
@ -62,13 +62,13 @@ public class SessionCenter {
|
|||
* 根据会话ID获取会话信息
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @return {@link SocketSession <GettySocketOperator>}
|
||||
* @return {@link SocketSession <GunsSocketOperator>}
|
||||
* @author majianguo
|
||||
* @date 2021/6/1 下午1:48
|
||||
**/
|
||||
public static SocketSession<GettySocketOperator> getSessionBySessionId(String sessionId) {
|
||||
for (List<SocketSession<GettySocketOperator>> values : socketSessionMap.values()) {
|
||||
for (SocketSession<GettySocketOperator> session : values) {
|
||||
public static SocketSession<GunsSocketOperator> getSessionBySessionId(String sessionId) {
|
||||
for (List<SocketSession<GunsSocketOperator>> values : socketSessionMap.values()) {
|
||||
for (SocketSession<GunsSocketOperator> session : values) {
|
||||
if (sessionId.equals(session.getSessionId())) {
|
||||
return session;
|
||||
}
|
||||
|
@ -84,8 +84,8 @@ public class SessionCenter {
|
|||
* @author majianguo
|
||||
* @date 2021/6/1 下午1:49
|
||||
**/
|
||||
public static void addSocketSession(SocketSession<GettySocketOperator> socketSession) {
|
||||
List<SocketSession<GettySocketOperator>> socketSessions = socketSessionMap.get(socketSession.getUserId());
|
||||
public static void addSocketSession(SocketSession<GunsSocketOperator> socketSession) {
|
||||
List<SocketSession<GunsSocketOperator>> socketSessions = socketSessionMap.get(socketSession.getUserId());
|
||||
if (ObjectUtil.isEmpty(socketSessions)) {
|
||||
socketSessions = new ArrayList<>();
|
||||
socketSessionMap.put(socketSession.getUserId(), socketSessions);
|
||||
|
@ -101,13 +101,13 @@ public class SessionCenter {
|
|||
* @date 2021/6/1 下午3:25
|
||||
**/
|
||||
public static void closed(String sessionId) {
|
||||
Set<Map.Entry<String, List<SocketSession<GettySocketOperator>>>> entrySet = socketSessionMap.entrySet();
|
||||
Iterator<Map.Entry<String, List<SocketSession<GettySocketOperator>>>> iterator = entrySet.iterator();
|
||||
Set<Map.Entry<String, List<SocketSession<GunsSocketOperator>>>> entrySet = socketSessionMap.entrySet();
|
||||
Iterator<Map.Entry<String, List<SocketSession<GunsSocketOperator>>>> iterator = entrySet.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<String, List<SocketSession<GettySocketOperator>>> next = iterator.next();
|
||||
List<SocketSession<GettySocketOperator>> value = next.getValue();
|
||||
Map.Entry<String, List<SocketSession<GunsSocketOperator>>> next = iterator.next();
|
||||
List<SocketSession<GunsSocketOperator>> value = next.getValue();
|
||||
if (ObjectUtil.isNotEmpty(value)) {
|
||||
value.removeIf(gettySocketOperatorSocketSession -> gettySocketOperatorSocketSession.getSessionId().equals(sessionId));
|
||||
value.removeIf(GunsSocketOperatorSocketSession -> GunsSocketOperatorSocketSession.getSessionId().equals(sessionId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue