代码规范性修改

pull/22/head
rays 2021-06-07 14:01:41 +08:00
parent 0a88f66f16
commit e23fc5d24b
10 changed files with 93 additions and 75 deletions

View File

@ -29,17 +29,15 @@ import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.auth.api.enums.DataScopeTypeEnum;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleRoleInfo;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.basic.SimpleUserInfo;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.scanner.api.annotation.field.ChineseDescription;
import cn.stylefeng.roses.kernel.socket.api.expander.SocketConfigExpander;
import lombok.Data;
import java.io.Serializable;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import static cn.stylefeng.roses.kernel.socket.api.constants.SocketConstants.SOCKET_PORT;
/**
*
*
@ -165,11 +163,10 @@ public class LoginUser implements Serializable {
public String getWsUrl() {
AtomicReference<String> returnUrl = new AtomicReference<>(StrUtil.EMPTY);
Integer socketPort = ConfigContext.me().getSysConfigValueWithDefault(SOCKET_PORT, Integer.class, 11130);
Optional.ofNullable(this.wsUrl).ifPresent(url -> {
Map<String, Long> user = new HashMap<>(1);
user.put("userId", this.userId);
user.put("port", Long.valueOf(socketPort));
user.put("port", Long.valueOf(SocketConfigExpander.getSocketPort()));
returnUrl.set(StrUtil.format(url, user));
});
return returnUrl.get();

View File

@ -42,19 +42,4 @@ public interface SocketConstants {
*/
String SOCKET_EXCEPTION_STEP_CODE = "30";
/**
* Socket
*/
String SOCKET_HOST = "socket_host";
/**
* Socket
*/
String SOCKET_PORT = "socket_port";
/**
* Socket
*/
String SOCKET_SERVER_CHUNK_SIZE = "socket_server_chunk_size";
}

View File

@ -5,7 +5,7 @@ import lombok.Getter;
/**
*
* <p>
*
* 使
*
* @author majianguo
* @date 2021/6/3 9:14
@ -19,10 +19,9 @@ public enum ClientMessageTypeEnum {
USER_ADD_MSG_TYPE("200001", "用户添加一个监听的消息类型"),
/**
*
*
*/
USER_HEART("299999", "用户心跳"),
;
USER_HEART("299999", "用户心跳消息类型");
private final String code;

View File

@ -4,6 +4,8 @@ import lombok.Getter;
/**
*
* <p>
* 使
*
* @author majianguo
* @date 2021/6/3 9:14
@ -14,8 +16,7 @@ public enum ServerMessageTypeEnum {
/**
*
*/
SYS_NOTICE_MSG_TYPE("100001", "系统通知消息类型"),
;
SYS_NOTICE_MSG_TYPE("100001", "系统通知消息类型");
private final String code;

View File

@ -46,9 +46,7 @@ public enum SocketExceptionEnum implements AbstractExceptionEnum {
/**
*
*/
SESSION_NOT_EXIST(RuleConstants.THIRD_ERROR_TYPE_CODE + SocketConstants.SOCKET_EXCEPTION_STEP_CODE + "02", "会话不存在"),
;
SESSION_NOT_EXIST(RuleConstants.THIRD_ERROR_TYPE_CODE + SocketConstants.SOCKET_EXCEPTION_STEP_CODE + "02", "会话不存在");
/**
*

View File

@ -0,0 +1,70 @@
/*
* Copyright [2020-2030] [https://www.stylefeng.cn]
*
* 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.
*
* GunsAPACHE LICENSE 2.0使
*
* 1.LICENSE
* 2.Guns
* 3.
* 4. https://gitee.com/stylefeng/guns
* 5. https://gitee.com/stylefeng/guns
* 6.
*/
package cn.stylefeng.roses.kernel.socket.api.expander;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
/**
* socket
*
* @author majianguo
* @date 2021/6/7 11:36
*/
public class SocketConfigExpander {
/**
*
*
* @return {@link java.lang.String}
* @author majianguo
* @date 2021/6/7 11:39
**/
public static String getSocketHost() {
return ConfigContext.me().getSysConfigValueWithDefault("socket_host", String.class, "0.0.0.0");
}
/**
*
*
* @return {@link java.lang.Integer}
* @author majianguo
* @date 2021/6/7 11:41
**/
public static Integer getSocketPort() {
return ConfigContext.me().getSysConfigValueWithDefault("socket_port", Integer.class, 11130);
}
/**
*
*
* @return {@link java.lang.Integer}
* @author majianguo
* @date 2021/6/7 11:41
**/
public static Integer getSocketServerChunkSize() {
return ConfigContext.me().getSysConfigValueWithDefault("socket_server_chunk_size", Integer.class, 512 * 1024 * 1024);
}
}

View File

@ -1,13 +1,8 @@
package cn.stylefeng.roses.kernel.socket.business.websocket.spring;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.config.api.constants.ConfigConstants;
import cn.stylefeng.roses.kernel.config.api.context.ConfigContext;
import cn.stylefeng.roses.kernel.socket.api.SocketOperatorApi;
import cn.stylefeng.roses.kernel.socket.api.enums.ServerMessageTypeEnum;
import cn.stylefeng.roses.kernel.socket.api.session.pojo.SocketSession;
import cn.stylefeng.roses.kernel.socket.api.expander.SocketConfigExpander;
import cn.stylefeng.roses.kernel.socket.websocket.message.WebSocketMessagePOJO;
import cn.stylefeng.roses.kernel.socket.websocket.operator.channel.GettySocketOperator;
import cn.stylefeng.roses.kernel.socket.websocket.server.WebSocketServer;
import cn.stylefeng.roses.kernel.socket.websocket.session.SessionCenter;
import com.gettyio.core.channel.config.ServerConfig;
@ -18,10 +13,7 @@ import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import java.net.StandardSocketOptions;
import java.util.HashSet;
import java.util.List;
import static cn.stylefeng.roses.kernel.socket.api.constants.SocketConstants.*;
import static cn.stylefeng.roses.kernel.socket.api.enums.ClientMessageTypeEnum.*;
/**
@ -38,22 +30,19 @@ public class WebSocketApplicationRunnerImpl implements ApplicationRunner {
private SocketOperatorApi socketOperatorApi;
@Override
public void run(ApplicationArguments args) throws Exception {
public void run(ApplicationArguments args) {
// 初始化配置对象
ServerConfig aioServerConfig = new ServerConfig();
// 设置host,不设置默认0.0.0.0
String socketHost = ConfigContext.me().getSysConfigValueWithDefault(SOCKET_HOST, String.class, "0.0.0.0");
aioServerConfig.setHost(socketHost);
// 设置host,默认0.0.0.0
aioServerConfig.setHost(SocketConfigExpander.getSocketHost());
// 设置端口号
Integer socketPort = ConfigContext.me().getSysConfigValueWithDefault(SOCKET_PORT, Integer.class, 11130);
aioServerConfig.setPort(socketPort);
// 设置端口号,默认11130
aioServerConfig.setPort(SocketConfigExpander.getSocketPort());
// 设置服务器端内存池最大可分配空间大小默认512mb内存池空间可以根据吞吐量设置。
// 尽量可以设置大一点,因为这不会真正的占用系统内存,只有真正使用时才会分配
Integer socketServerChunkSize = ConfigContext.me().getSysConfigValueWithDefault(SOCKET_SERVER_CHUNK_SIZE, Integer.class, 512 * 1024 * 1024);
aioServerConfig.setServerChunkSize(socketServerChunkSize);
aioServerConfig.setServerChunkSize(SocketConfigExpander.getSocketServerChunkSize());
// 设置SocketOptions
aioServerConfig.setOption(StandardSocketOptions.SO_RCVBUF, 8192);

View File

@ -1,19 +1,3 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 cn.stylefeng.roses.kernel.socket.websocket.server;
import cn.stylefeng.roses.kernel.socket.websocket.server.handler.WebSocketMessageHandler;
@ -32,7 +16,7 @@ import com.gettyio.expansion.handler.codec.websocket.WebSocketEncoder;
public class WebSocketInitializer extends ChannelInitializer {
@Override
public void initChannel(SocketChannel channel) throws Exception {
public void initChannel(SocketChannel channel) {
// 获取责任链对象
DefaultChannelPipeline pipeline = channel.getDefaultChannelPipeline();

View File

@ -14,7 +14,7 @@ import java.net.StandardSocketOptions;
public class WebSocketServer {
/**
*
* (使)
*
* @author majianguo
* @date 2021/6/2 11:10

View File

@ -1,8 +1,6 @@
package cn.stylefeng.roses.kernel.socket.websocket.server.handler;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.constants.RuleConstants;
import cn.stylefeng.roses.kernel.rule.constants.SymbolConstant;
import cn.stylefeng.roses.kernel.socket.api.enums.ClientMessageTypeEnum;
import cn.stylefeng.roses.kernel.socket.api.message.SocketMsgCallbackInterface;
import cn.stylefeng.roses.kernel.socket.websocket.message.SocketMessageCenter;
@ -16,8 +14,7 @@ import com.gettyio.core.channel.SocketChannel;
import com.gettyio.core.pipeline.in.SimpleChannelInboundHandler;
import com.gettyio.expansion.handler.codec.websocket.frame.TextWebSocketFrame;
import com.gettyio.expansion.handler.codec.websocket.frame.WebSocketFrame;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@ -27,18 +24,17 @@ import java.nio.charset.StandardCharsets;
* @author majianguo
* @date 2021/6/1 2:35
*/
@Slf4j
public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
private final Logger log = LoggerFactory.getLogger(WebSocketMessageHandler.class);
@Override
public void channelAdded(SocketChannel aioChannel) throws Exception {
public void channelAdded(SocketChannel aioChannel) {
log.info(aioChannel.getChannelId() + " connection successful.");
ChannelIdAndUserBindCenter.addSocketChannel(aioChannel);
}
@Override
public void channelClosed(SocketChannel aioChannel) throws Exception {
public void channelClosed(SocketChannel aioChannel) {
log.info(aioChannel.getChannelId() + " disconnected");
// 获取用户ID
String userId = ChannelIdAndUserBindCenter.getUserId(aioChannel.getChannelId());
@ -50,7 +46,7 @@ public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSock
}
@Override
public void channelRead0(SocketChannel socketChannel, WebSocketFrame webSocketFrame) throws Exception {
public void channelRead0(SocketChannel socketChannel, WebSocketFrame webSocketFrame) {
if (webSocketFrame instanceof TextWebSocketFrame) {
String data = new String(webSocketFrame.getPayloadData(), StandardCharsets.UTF_8);
@ -82,7 +78,6 @@ public class WebSocketMessageHandler extends SimpleChannelInboundHandler<WebSock
socketSession.setUserId(webSocketMessagePOJO.getFormUserId());
socketSession.setSocketOperatorApi(new GettySocketOperator(socketChannel));
socketSession.setConnectionTime(System.currentTimeMillis());
socketSession.setLastActiveTime(System.currentTimeMillis());
// 维护会话
SessionCenter.addSocketSession(socketSession);