update websocket 异常状态码新增
This commit is contained in:
parent
e51364f20d
commit
6db9f1a7b4
|
@ -0,0 +1,38 @@
|
|||
package com.m2pool.chat.constant;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @Enums ExceptionEnum
|
||||
* @Description 自定义websocket 异常码枚举类
|
||||
* @Author yyb
|
||||
* @Date 2025/4/17 10:18
|
||||
*/
|
||||
@Getter
|
||||
public enum ExceptionEnum {
|
||||
IP_LIMIT_CONNECT(1020, "本机连接数已达上限,请关闭已有链接"),
|
||||
MAX_LIMIT_CONNECT(1021, "服务器websocket连接数已达上限,服务器拒绝链接"),
|
||||
SET_PRINCIPAL_FAIL(1022, "websocket链接异常,用户身份设置失败"),
|
||||
GET_PRINCIPAL_FAIL(1023, "websocket链接异常,用户信息邮箱获取失败"),
|
||||
;
|
||||
|
||||
private final int code;
|
||||
private final String description;
|
||||
|
||||
ExceptionEnum(int code, String description) {
|
||||
this.code = code;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据枚举类,拼接一个返回给前端的状态信息
|
||||
* @param exceptionEnum 枚举类
|
||||
* @return 返回一个状态信息,供前端解析
|
||||
*/
|
||||
public static String fromCode(ExceptionEnum exceptionEnum) {
|
||||
int code = exceptionEnum.getCode();
|
||||
String description = exceptionEnum.getDescription();
|
||||
return code+","+description;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
package com.m2pool.chat.coverter;
|
||||
|
||||
import com.m2pool.chat.exception.WebSocketException;
|
||||
import com.m2pool.common.core.utils.JsonUtil;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageDeliveryException;
|
||||
import org.springframework.messaging.MessageHeaders;
|
||||
import org.springframework.messaging.converter.MessageConverter;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
@ -29,7 +29,7 @@ public class CommonMessageConvert implements MessageConverter {
|
|||
String textPayload = new String((byte[]) message.getPayload(), StandardCharsets.UTF_8);
|
||||
return JsonUtil.convertString2Object(textPayload,targetClass);
|
||||
} catch (Exception e) {
|
||||
throw new WebSocketException( "消息格式错误");
|
||||
throw new MessageDeliveryException( "消息格式错误");
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
package com.m2pool.chat.exception;
|
||||
|
||||
import com.m2pool.common.core.exception.base.BaseException;
|
||||
|
||||
public class WebSocketException extends BaseException {
|
||||
|
||||
public WebSocketException(String module, String code, Object[] args, String defaultMessage) {
|
||||
super(module, code, args, defaultMessage);
|
||||
}
|
||||
|
||||
public WebSocketException(String module, String code, Object[] args) {
|
||||
super(module, code, args);
|
||||
}
|
||||
|
||||
public WebSocketException(String module, String defaultMessage) {
|
||||
super(module, defaultMessage);
|
||||
}
|
||||
|
||||
public WebSocketException(String code, Object[] args) {
|
||||
super(code, args);
|
||||
}
|
||||
|
||||
public WebSocketException(String defaultMessage) {
|
||||
super(defaultMessage);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,7 @@ package com.m2pool.chat.interceptor;
|
|||
|
||||
|
||||
import com.m2pool.chat.config.CustomWebSocketConfig;
|
||||
import com.m2pool.chat.constant.ExceptionEnum;
|
||||
import com.m2pool.chat.entity.StompPrincipal;
|
||||
import lombok.Data;
|
||||
import org.slf4j.Logger;
|
||||
|
@ -67,6 +68,9 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor {
|
|||
StompHeaderAccessor accessor = StompHeaderAccessor.wrap(message);
|
||||
if (accessor.getCommand() == StompCommand.CONNECT ) {
|
||||
StompHeaderAccessor mha = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
|
||||
if(mha == null){
|
||||
throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.SET_PRINCIPAL_FAIL));
|
||||
}
|
||||
Object raw = message.getHeaders().get(SimpMessageHeaderAccessor.NATIVE_HEADERS);
|
||||
int type = Integer.parseInt(getConnectCustomHeaders(raw, TYPE));
|
||||
String email = getConnectCustomHeaders(raw, EMAIL);
|
||||
|
@ -76,9 +80,7 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor {
|
|||
//根据客服端ip + 用户类型限制连接数
|
||||
ipLimit(accessor,type);
|
||||
|
||||
if(mha == null){
|
||||
throw new MessageDeliveryException("get MessageHeaderAccessor failed,can not set Principal,connect failed");
|
||||
}
|
||||
|
||||
//链接请求头中用户信息存入stomp中
|
||||
mha.setUser(new StompPrincipal(email,type));
|
||||
}
|
||||
|
@ -107,7 +109,7 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor {
|
|||
private void maxConnectionsLimit(){
|
||||
if(connectionCount.incrementAndGet() >= customWebSocketConfig.getMaxConnections()){
|
||||
LOGGER.info("reach max connections,refuse connect");
|
||||
throw new MessageDeliveryException("reach max connections,refuse connect");
|
||||
throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.MAX_LIMIT_CONNECT));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -120,11 +122,11 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor {
|
|||
if (sessionAttributes != null) {
|
||||
String ipAddr = (String) sessionAttributes.get(IPADDR);
|
||||
if(type != CUSTOMER && ipConnectionCountMap.get(ipAddr) != null){
|
||||
throw new MessageDeliveryException("this ip maximum number of connections is reached");
|
||||
throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.IP_LIMIT_CONNECT));
|
||||
}
|
||||
ipConnectionCountMap.put(ipAddr,true);
|
||||
}else{
|
||||
throw new MessageDeliveryException("not find email and type headers");
|
||||
throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.GET_PRINCIPAL_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -159,7 +161,7 @@ public class WebsocketChannelInterceptor implements ChannelInterceptor {
|
|||
ipConnectionCountMap.remove(ipAddr);
|
||||
}
|
||||
}else{
|
||||
throw new MessageDeliveryException("not find email and type headers");
|
||||
throw new MessageDeliveryException(ExceptionEnum.fromCode(ExceptionEnum.GET_PRINCIPAL_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,5 +9,4 @@ package com.m2pool.chat.task;
|
|||
public class ChatTask {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue