mirror of https://gitee.com/stylefeng/roses
代码规范性修改
parent
0a88f66f16
commit
e23fc5d24b
|
@ -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();
|
||||
|
|
|
@ -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";
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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", "会话不存在");
|
||||
|
||||
/**
|
||||
* 错误编码
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
* Guns采用APACHE 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ import java.net.StandardSocketOptions;
|
|||
public class WebSocketServer {
|
||||
|
||||
/**
|
||||
* 无参数启动
|
||||
* 无参数启动(开发测试使用)
|
||||
*
|
||||
* @author majianguo
|
||||
* @date 2021/6/2 上午11:10
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue