🎉 优化系统架构.

This commit is contained in:
lijiahangmax
2025-06-25 18:59:16 +08:00
parent cec7e21d5a
commit f69093de66
15 changed files with 263 additions and 26 deletions

View File

@@ -122,4 +122,9 @@ public class OperatorLogModel implements RequestIdentity {
*/
private Date endTime;
/**
* 创建时间
*/
private Date createTime;
}

View File

@@ -49,6 +49,8 @@ public class CacheBarriers {
public static final GenericsBarrier<Map<?, ?>> MAP = GenericsAnonymousMapBarrier.create(Const.NONE_ID, Const.NONE_ID);
public static final GenericsBarrier<Map<?, ?>> STRING_MAP = GenericsAnonymousMapBarrier.create(Const.NONE_ID.toString(), Const.NONE_ID);
/**
* 创建屏障对象 防止穿透
*

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.visor.framework.websocket.core.constant;
/**
* ws 服务端关闭 code
* <p>
* > 2999 && < 5000
*
* @author Jiahang Li
* @version 1.0.0
* @since 2021/6/16 15:18
*/
public interface CloseCode {
/**
* code
*
* @return code
*/
int getCode();
/**
* reason
*
* @return reason
*/
String getReason();
}

View File

@@ -22,27 +22,36 @@
*/
package org.dromara.visor.framework.websocket.core.constant;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* ws 服务端关闭 code
* ws 关闭码
* <p>
* > 2999 && < 5000
*
* @author Jiahang Li
* @version 1.0.0
* @since 2021/6/16 15:18
* @since 2024/7/31 17:41
*/
public interface WsCloseCode {
@Getter
@AllArgsConstructor
public enum WsCloseCode implements CloseCode {
/**
* code
*
* @return code
* 初始化失败
*/
int getCode();
INIT_ERROR(3000, "init error"),
/**
* reason
*
* @return reason
* 会话已关闭
*/
String getReason();
SESSION_CLOSED(3100, "session closed"),
;
private final int code;
private final String reason;
}

View File

@@ -22,18 +22,23 @@
*/
package org.dromara.visor.framework.websocket.core.utils;
import cn.orionsec.kit.lang.constant.StandardHttpHeader;
import cn.orionsec.kit.lang.utils.Exceptions;
import cn.orionsec.kit.lang.utils.Threads;
import cn.orionsec.kit.lang.utils.io.Streams;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.common.constant.Const;
import org.dromara.visor.framework.websocket.core.constant.WsCloseCode;
import org.dromara.visor.framework.websocket.core.constant.CloseCode;
import org.dromara.visor.framework.websocket.core.session.WebSocketSyncSession;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import java.io.IOException;
import java.util.List;
/**
* websocket 工具类
@@ -132,18 +137,54 @@ public class WebSockets {
}
}
/**
* 设置子协议
*
* @param request request
* @param response response
*/
public static void setSubProtocols(ServerHttpRequest request, ServerHttpResponse response) {
List<String> subProtocols = request.getHeaders().get(StandardHttpHeader.SEC_WEBSOCKET_PROTOCOL);
if (subProtocols != null) {
response.getHeaders().put(StandardHttpHeader.SEC_WEBSOCKET_PROTOCOL, subProtocols);
}
}
/**
* 关闭会话
*
* @param session session
*/
public static void close(WebSocketSession session) {
if (!session.isOpen()) {
return;
}
Streams.close(session);
}
/**
* 关闭会话
*
* @param session session
* @param code code
*/
public static void close(WebSocketSession session, WsCloseCode code) {
public static void close(WebSocketSession session, CloseCode code) {
close(session, code.getCode(), code.getReason());
}
/**
* 关闭会话
*
* @param session session
* @param code code
* @param reason reason
*/
public static void close(WebSocketSession session, int code, String reason) {
if (!session.isOpen()) {
return;
}
try {
session.close(new CloseStatus(code.getCode(), code.getReason()));
session.close(new CloseStatus(code, reason));
} catch (Exception e) {
log.error("websocket close failure", e);
}