mirror of https://gitee.com/stylefeng/roses
修复WebSocket关闭报空指针问题
parent
859983217f
commit
8da206a0a0
|
@ -70,7 +70,7 @@ public class WebSocketOperator implements SocketOperatorApi {
|
|||
|
||||
@Override
|
||||
public void closeSocketBySocketId(String socketId) {
|
||||
SessionCenter.closed(socketId);
|
||||
SessionCenter.closed(socketId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -104,12 +104,12 @@ public class WebSocketServer {
|
|||
SocketSession<GunsSocketOperator> socketSession = SessionCenter.getSessionBySessionId(session.getId());
|
||||
// 触发首次连接回调
|
||||
SocketMsgCallbackInterface socketMsgCallbackInterface = SocketMessageCenter.getSocketMsgCallbackInterface(SystemMessageTypeEnum.SYS_LISTENER_ONCLOSE.getCode());
|
||||
if (ObjectUtil.isNotEmpty(socketMsgCallbackInterface)) {
|
||||
if (ObjectUtil.isNotEmpty(socketSession) && ObjectUtil.isNotEmpty(socketMsgCallbackInterface)) {
|
||||
// 触发回调
|
||||
socketMsgCallbackInterface.callback(SystemMessageTypeEnum.SYS_LISTENER_ONCLOSE.getCode(), null, socketSession);
|
||||
}
|
||||
} finally {
|
||||
SessionCenter.closed(session.getId());
|
||||
SessionCenter.deleteById(session.getId());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -67,10 +67,12 @@ public class SessionCenter {
|
|||
* @date 2021/6/1 下午1:48
|
||||
**/
|
||||
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;
|
||||
Set<Map.Entry<String, List<SocketSession<GunsSocketOperator>>>> entrySet = socketSessionMap.entrySet();
|
||||
for (Map.Entry<String, List<SocketSession<GunsSocketOperator>>> stringListEntry : entrySet) {
|
||||
List<SocketSession<GunsSocketOperator>> stringListEntryValue = stringListEntry.getValue();
|
||||
for (SocketSession<GunsSocketOperator> socketSession : stringListEntryValue) {
|
||||
if (sessionId.equals(socketSession.getSessionId())) {
|
||||
return socketSession;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -101,14 +103,43 @@ public class SessionCenter {
|
|||
* @date 2021/6/1 下午3:25
|
||||
**/
|
||||
public static void closed(String sessionId) {
|
||||
// 删除维护关系
|
||||
SocketSession<GunsSocketOperator> operatorSocketSession = getSessionBySessionId(sessionId);
|
||||
if (ObjectUtil.isNotEmpty(operatorSocketSession)) {
|
||||
operatorSocketSession.getSocketOperatorApi().close();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除维护关系
|
||||
*
|
||||
* @param sessionId 会话ID
|
||||
* @return {@link cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession<cn.stylefeng.roses.kernel.socket.business.websocket.operator.channel.GunsSocketOperator>}
|
||||
* @author majianguo
|
||||
* @date 2021/8/30 9:20
|
||||
**/
|
||||
public static SocketSession<GunsSocketOperator> deleteById(String sessionId) {
|
||||
// 获取所有人员的连接会话信息
|
||||
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<GunsSocketOperator>>> next = iterator.next();
|
||||
List<SocketSession<GunsSocketOperator>> value = next.getValue();
|
||||
if (ObjectUtil.isNotEmpty(value)) {
|
||||
value.removeIf(GunsSocketOperatorSocketSession -> GunsSocketOperatorSocketSession.getSessionId().equals(sessionId));
|
||||
|
||||
// 找出该人员的指定会话信息
|
||||
Iterator<SocketSession<GunsSocketOperator>> socketSessionIterator = value.iterator();
|
||||
while (socketSessionIterator.hasNext()) {
|
||||
SocketSession<GunsSocketOperator> operatorSocketSession = socketSessionIterator.next();
|
||||
if (operatorSocketSession.getSessionId().equals(sessionId)) {
|
||||
// 在列表中删除
|
||||
socketSessionIterator.remove();
|
||||
return operatorSocketSession;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue