diff --git a/README-CN.md b/README-CN.md
index 31f809c..46f2a43 100644
--- a/README-CN.md
+++ b/README-CN.md
@@ -1,4 +1,6 @@
-
+
+
+
这是一款使用netty编写的springboot websocket组件。
@@ -17,7 +19,7 @@
plus.jdk
spring-boot-starter-websocket
- 1.0.1
+ 1.0.2
```
## 配置
@@ -197,3 +199,43 @@ public class DemoHandler {
}
```
+### 使用websocket连接主动向用户客户端推送消息
+
+```java
+package plus.jdk.broadcast.test.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import plus.jdk.websocket.global.SessionGroupManager;
+import plus.jdk.websocket.model.IWsSession;
+
+import javax.annotation.Resource;
+import java.util.concurrent.ConcurrentLinkedDeque;
+
+
+@RestController
+public class MessageController {
+
+ /**
+ * 该bean实例已经在底层封装好
+ */
+ @Resource
+ private SessionGroupManager sessionGroupManager;
+
+ @RequestMapping(value = "/message/send", method = {RequestMethod.GET})
+ public Object sendMessage(@RequestParam String uid, @RequestParam String content){
+
+ // 调用sessionGroupManager.getSession()函数获取当前用户在该实例中的所有连接
+ // 你可以在 IWSSessionAuthenticator 的实现中自行实现自己的session定义,将消息分发给不同的设备
+ // 或向远端上报当前用户的连接到底在哪些机器上
+ ConcurrentLinkedDeque> sessions = sessionGroupManager.getSession(uid, "/ws/message");
+ for(IWsSession> wsSession: sessions) {
+ wsSession.sendText(content); // 发送文本消息
+ wsSession.sendBinary(content.getBytes()); // 发送二进制消息
+ }
+ return "success";
+ }
+}
+```
diff --git a/README.md b/README.md
index 1e954cc..32f91e1 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,6 @@
+
+
+
A springboot websocket component written using netty。
@@ -14,7 +17,7 @@
plus.jdk
spring-boot-starter-websocket
- 1.0.1
+ 1.0.2
```
@@ -195,3 +198,43 @@ public class DemoHandler {
}
```
+### Actively push messages to user clients using websocket connections
+
+```java
+package plus.jdk.broadcast.test.controller;
+
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import plus.jdk.websocket.global.SessionGroupManager;
+import plus.jdk.websocket.model.IWsSession;
+
+import javax.annotation.Resource;
+import java.util.concurrent.ConcurrentLinkedDeque;
+
+
+@RestController
+public class MessageController {
+
+ /**
+ * The bean instance is already encapsulated at the bottom
+ */
+ @Resource
+ private SessionGroupManager sessionGroupManager;
+
+ @RequestMapping(value = "/message/send", method = {RequestMethod.GET})
+ public Object sendMessage(@RequestParam String uid, @RequestParam String content){
+
+ // Call the sessionGroupManager.getSession() function to get all connections of the current user in this instance.
+ // You can implement your own session definition in the implementation of IWSSessionAuthenticator to distribute messages to different devices
+ // Or report to the remote end which machines the current user is connected to
+ ConcurrentLinkedDeque> sessions = sessionGroupManager.getSession(uid, "/ws/message");
+ for(IWsSession> wsSession: sessions) {
+ wsSession.sendText(content); // 发送文本消息
+ wsSession.sendBinary(content.getBytes()); // 发送二进制消息
+ }
+ return "success";
+ }
+}
+```
diff --git a/pom.xml b/pom.xml
index a519a2c..2492e77 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
plus.jdk
spring-boot-starter-websocket
- 1.0.1
+ 1.0.2
spring-boot-starter-websocket
A simple websocket component base on netty
diff --git a/src/main/java/plus/jdk/websocket/config/WebsocketAutoConfiguration.java b/src/main/java/plus/jdk/websocket/config/WebsocketAutoConfiguration.java
index f238c76..2d122df 100644
--- a/src/main/java/plus/jdk/websocket/config/WebsocketAutoConfiguration.java
+++ b/src/main/java/plus/jdk/websocket/config/WebsocketAutoConfiguration.java
@@ -13,6 +13,7 @@
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.SessionGroupManager;
import plus.jdk.websocket.global.WebsocketDispatcher;
import plus.jdk.websocket.annotations.*;
import plus.jdk.websocket.properties.WebsocketProperties;
@@ -34,6 +35,11 @@ public class WebsocketAutoConfiguration extends WebApplicationObjectSupport impl
public WebsocketAutoConfiguration(WebsocketProperties properties) {
}
+ @Bean
+ public SessionGroupManager SessionGroupManager() {
+ return new SessionGroupManager();
+ }
+
@Bean
public WebsocketDispatcher WebsocketDispatcher(WebsocketProperties properties){
return new WebsocketDispatcher(properties, beanFactory);
diff --git a/src/main/java/plus/jdk/websocket/global/HttpServerHandler.java b/src/main/java/plus/jdk/websocket/global/HttpServerHandler.java
index 91a2ab1..76ac67b 100644
--- a/src/main/java/plus/jdk/websocket/global/HttpServerHandler.java
+++ b/src/main/java/plus/jdk/websocket/global/HttpServerHandler.java
@@ -14,6 +14,7 @@
import io.netty.util.AttributeKey;
import io.netty.util.CharsetUtil;
import org.springframework.beans.TypeMismatchException;
+import org.springframework.beans.factory.BeanFactory;
import org.springframework.util.StringUtils;
import plus.jdk.websocket.common.WebsocketCommonException;
import plus.jdk.websocket.properties.WebsocketProperties;
@@ -31,10 +32,14 @@ public class HttpServerHandler extends SimpleChannelInboundHandler {
if (future.isSuccess()) {
diff --git a/src/main/java/plus/jdk/websocket/global/SessionGroupManager.java b/src/main/java/plus/jdk/websocket/global/SessionGroupManager.java
new file mode 100644
index 0000000..f12b147
--- /dev/null
+++ b/src/main/java/plus/jdk/websocket/global/SessionGroupManager.java
@@ -0,0 +1,49 @@
+package plus.jdk.websocket.global;
+
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandlerContext;
+import lombok.extern.slf4j.Slf4j;
+import plus.jdk.websocket.model.ChannelModel;
+import plus.jdk.websocket.model.IWsSession;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedDeque;
+
+@Slf4j
+public class SessionGroupManager {
+
+ private final ConcurrentHashMap channelModelMap = new ConcurrentHashMap<>();
+
+ private final ConcurrentHashMap