Skip to content

Commit

Permalink
添加详细的配置说明
Browse files Browse the repository at this point in the history
  • Loading branch information
[moon] committed Jul 17, 2022
1 parent 1ccc6cf commit 44446db
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 30 deletions.
47 changes: 42 additions & 5 deletions README-CN.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

<h3 align="center">这是一款使用netty编写的springboot websocket组件。</h3>
<p align="center">
<a href="https://github.com/JDK-Plus/cli-plus/blob/master/LICENSE"><img src="https://img.shields.io/github/license/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/cli-plus/releases"><img src="https://img.shields.io/github/release/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/cli-plus/stargazers"><img src="https://img.shields.io/github/stars/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/cli-plus/network/members"><img src="https://img.shields.io/github/forks/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/blob/master/LICENSE"><img src="https://img.shields.io/github/license/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/releases"><img src="https://img.shields.io/github/release/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/stargazers"><img src="https://img.shields.io/github/stars/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/network/members"><img src="https://img.shields.io/github/forks/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
</p>


Expand All @@ -24,9 +24,46 @@

```
plus.jdk.websocket.enabled=true
# 指定host
plus.jdk.websocket.host=0.0.0.0
# 指定websocket端口
plus.jdk.websocket.port=10001
plus.jdk.websocket.cors-allow-credentials=true
# 指定自定义实现的验证器
plus.jdk.websocket.session-authenticator=plus.jdk.broadcast.test.session.WSSessionAuthenticator
# boss线程池线程数,默认为1
plus.jdk.websocket.boss-loop-group-threads=1
# worker线程池线程数,若不指定则默认为CPU核心数 * 2
plus.jdk.websocket.worker-loop-group-threads=5
# 是否需允许跨域
plus.jdk.websocket.cors-allow-credentials=true
# 跨域的header头
plus.jdk.websocket.cors-origins[0]=""
# 是否使用 NioEventLoopGroup 来处理请求
plus.jdk.websocket.use-event-executor-group=true
# 指定 NioEventLoopGroup 线程池数量
plus.jdk.websocket.event-executor-group-threads=0
# 连接超时时间
#plus.jdk.websocket.connect-timeout-millis=
# 指定了内核为此套接口排队的最大连接个数
#plus.jdk.websocket.SO_BACKLOG=
# 旋转计数用于控制每次Netty写入操作调用基础socket.write(...)的次数
#plus.jdk.websocket.write-spin-count=
# 日志等级
plus.jdk.websocket.log-level=debug
```


Expand Down
48 changes: 43 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<h3 align="center">A springboot websocket component written using netty。</h3>
<p align="center">
<a href="https://github.com/JDK-Plus/cli-plus/blob/master/LICENSE"><img src="https://img.shields.io/github/license/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/cli-plus/releases"><img src="https://img.shields.io/github/release/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/cli-plus/stargazers"><img src="https://img.shields.io/github/stars/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/cli-plus/network/members"><img src="https://img.shields.io/github/forks/JDK-Plus/cli-plus.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/blob/master/LICENSE"><img src="https://img.shields.io/github/license/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/releases"><img src="https://img.shields.io/github/release/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/stargazers"><img src="https://img.shields.io/github/stars/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
<a href="https://github.com/JDK-Plus/spring-boot-starter-websocket/network/members"><img src="https://img.shields.io/github/forks/JDK-Plus/spring-boot-starter-websocket.svg" /></a>
</p>

- [中文文档](README.md)
- [中文文档](README-CN.md)

## Maven Dependencies

Expand All @@ -22,8 +22,46 @@

```
plus.jdk.websocket.enabled=true
# Specify host
plus.jdk.websocket.host=0.0.0.0
# Specify the websocket port
plus.jdk.websocket.port=10001
# Specify a custom-implemented validator
plus.jdk.websocket.session-authenticator=plus.jdk.broadcast.test.session.WSSessionAuthenticator
# The number of threads in the boss thread pool, the default is 1
plus.jdk.websocket.boss-loop-group-threads=1
# The number of worker thread pool threads, if not specified, the default is the number of CPU cores * 2
plus.jdk.websocket.worker-loop-group-threads=5
# Whether to allow cross-domain
plus.jdk.websocket.cors-allow-credentials=true
# cross-domain header
plus.jdk.websocket.cors-origins[0]=""
# Whether to use NioEventLoopGroup to handle requests
plus.jdk.websocket.use-event-executor-group=true
# Specify the number of NioEventLoopGroup thread pools
plus.jdk.websocket.event-executor-group-threads=0
# connection timeout
#plus.jdk.websocket.connect-timeout-millis=
# Specifies the maximum number of connections that the kernel queues for this socket
#plus.jdk.websocket.SO_BACKLOG=
# The spin count is used to control how many times the underlying socket.write(...) is called per Netty write operation
#plus.jdk.websocket.write-spin-count=
# log level
plus.jdk.websocket.log-level=debug
```

## Example of use
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.WebApplicationObjectSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import plus.jdk.websocket.global.ServerEndpointExporter;
import plus.jdk.websocket.global.WebsocketDispatcher;
import plus.jdk.websocket.annotations.*;
import plus.jdk.websocket.properties.WebsocketProperties;
Expand All @@ -38,6 +39,11 @@ public WebsocketDispatcher WebsocketDispatcher(WebsocketProperties properties){
return new WebsocketDispatcher(properties, beanFactory);
}

@Bean
public ServerEndpointExporter serverEndpointExporter(WebsocketProperties properties) {
return new ServerEndpointExporter(websocketDispatcher);
}

@Override
public void destroy() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
super.channelInactive(ctx);
}

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws WebsocketCommonException {
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
FullHttpResponse res;
// Handle a bad request.
if (!req.decoderResult().isSuccess()) {
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/plus/jdk/websocket/global/WebsocketDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class WebsocketDispatcher {

private static final AttributeKey<Object> POJO_KEY = AttributeKey.valueOf("WEBSOCKET_IMPLEMENT");

public static final AttributeKey<IWsSession> SESSION_KEY = AttributeKey.valueOf("WEBSOCKET_SESSION");
public static final AttributeKey<IWsSession<?>> SESSION_KEY = AttributeKey.valueOf("WEBSOCKET_SESSION");

private static final AttributeKey<String> PATH_KEY = AttributeKey.valueOf("WEBSOCKET_PATH");

Expand All @@ -67,10 +67,10 @@ public void startSocketServer() {
String[] corsOrigins = properties.getCorsOrigins();
Boolean corsAllowCredentials = properties.getCorsAllowCredentials();
CorsConfig corsConfig = createCorsConfig(corsOrigins, corsAllowCredentials);
NioEventLoopGroup boss = new NioEventLoopGroup(properties.getBossLoopGroupThreads());
NioEventLoopGroup master = new NioEventLoopGroup(properties.getBossLoopGroupThreads());
NioEventLoopGroup worker = new NioEventLoopGroup(properties.getWorkerLoopGroupThreads());
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(boss, worker);
bootstrap.group(master, worker);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, properties.getConnectTimeoutMillis());
bootstrap.option(ChannelOption.SO_BACKLOG, properties.getSO_BACKLOG());
Expand Down Expand Up @@ -108,7 +108,7 @@ protected void initChannel(SocketChannel ch) throws Exception {
}
});
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
boss.shutdownGracefully().syncUninterruptibly();
master.shutdownGracefully().syncUninterruptibly();
worker.shutdownGracefully().syncUninterruptibly();
}));
}
Expand Down Expand Up @@ -148,16 +148,14 @@ public boolean hasBeforeHandshake(Channel channel, String path) {
/**
* 执行握手
*/
public void doBeforeHandshake(Channel channel, FullHttpRequest req, String path) throws WebsocketCommonException {
public void doBeforeHandshake(Channel channel, FullHttpRequest req, String path) throws Exception {
WebsocketMethodMapping methodMapping = websocketMethodMap.get(path);
if(methodMapping == null) {
return;
}
Object implement = methodMapping.getBeanObject();
channel.attr(POJO_KEY).set(implement);
IWSSessionAuthenticator authenticator = beanFactory.getBean(properties.getSessionAuthenticator());
IWsSession wsSession = authenticator.authenticate(channel, req, path);
channel.attr(SESSION_KEY).set(wsSession);
setSession(channel, req, path);
channel.attr(PATH_KEY).set(path);
Method beforeHandshake = methodMapping.getBeforeHandshake();
if (beforeHandshake != null) {
Expand All @@ -171,17 +169,15 @@ public void doBeforeHandshake(Channel channel, FullHttpRequest req, String path)
}
}

public void doOnOpen(Channel channel, FullHttpRequest req, String path) throws WebsocketCommonException {
public void doOnOpen(Channel channel, FullHttpRequest req, String path) throws Exception {
WebsocketMethodMapping methodMapping = websocketMethodMap.get(path);
if(methodMapping == null) {
return;
}
Object implement = channel.attr(POJO_KEY).get();
if (implement==null){
implement = methodMapping.getBeanObject();
IWSSessionAuthenticator authenticator = beanFactory.getBean(properties.getSessionAuthenticator());
IWsSession wsSession = authenticator.authenticate(channel, req, path);
channel.attr(SESSION_KEY).set(wsSession);
setSession(channel, req, path);
}

Method onOpenMethod = methodMapping.getOnOpenMethod();
Expand All @@ -196,6 +192,12 @@ public void doOnOpen(Channel channel, FullHttpRequest req, String path) throws W
}
}

private void setSession(Channel channel, FullHttpRequest req, String path) throws Exception {
IWSSessionAuthenticator<?> authenticator = beanFactory.getBean(properties.getSessionAuthenticator());
IWsSession<?> wsSession = authenticator.authenticate(channel, req, path);
channel.attr(SESSION_KEY).set(wsSession);
}

public void doOnClose(Channel channel) {
Attribute<String> attrPath = channel.attr(PATH_KEY);
String path = attrPath.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,4 @@
@ConditionalOnMissingBean(ServerEndpointExporter.class)
public class WebsocketSelector {

@Resource
private WebsocketDispatcher websocketDispatcher;

@Bean
public ServerEndpointExporter serverEndpointExporter(WebsocketProperties properties) {
return new ServerEndpointExporter(websocketDispatcher);
}
}

0 comments on commit 44446db

Please sign in to comment.