Skip to content

Commit

Permalink
修复将二进制消息作为文本消息发送的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
[moon] committed Jul 23, 2022
1 parent 3d160c5 commit f29f1de
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
11 changes: 7 additions & 4 deletions README-CN.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<img align="center" src="https://jdk.plus/img/jdk-plus.png" alt="drawing" style="width:100%;"/>
<h3 align="center">这是一款使用netty编写的springboot websocket组件。</h3>
<h3 align="center">这是一款使用netty编写的springboot websocket 集群化消息推送的组件。</h3>
<p align="center">
<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>
<p align="center">这是一款支持集群广播的使用netty编写的websocket组件,完美解决websocket和用户单机无法和整个业务集群通信的问题</p>
<p align="center">这是一款支持集群广播的使用netty编写的websocket组件,完美解决websocket和用户单机建立连接无法和整个业务集群通信的问题</p>


- [English](README-CN.md)
Expand All @@ -18,7 +18,7 @@
<dependency>
<groupId>plus.jdk</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
```
## 配置
Expand Down Expand Up @@ -71,6 +71,9 @@ plus.jdk.websocket.broadcast-monitor-port=10300
# udp广播监听端口,若小于等于0,则不监听消息
plus.jdk.websocket.broadcast-monitor-port=10300
# 是否将接收到的UDP广播信息打印到日志中
plus.jdk.websocket.print-broadcast-message=true
```


Expand All @@ -83,7 +86,7 @@ plus.jdk.websocket.broadcast-monitor-port=10300

业务上session的定义必须实现`IWsSession` 接口。

该接口封装了一个`userId`,该参数用户可以自定义其类型。还有一个channel,后续用户与该对象的交互都依赖于这个对象
该接口封装了一个`userId`,该参数用户可以自定义其类型。还有一个channel,是当前机器与用户建立的连接

```java
package plus.jdk.broadcast.test.session;
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<img align="center" src="https://jdk.plus/img/jdk-plus.png" alt="drawing" style="width:100%;"/>
<h3 align="center">A springboot websocket component written using netty</h3>
<h3 align="center">A springboot websocket clustered message push component written by netty.</h3>
<p align="center">
<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>
<p align="center">This is a websocket component written in netty that supports cluster broadcasting, Perfectly solve the problem that websocket and user single machine cannot communicate with the entire business cluster</p>
<p align="center">This is a websocket component written with netty that supports cluster broadcasting, which perfectly solves the problem that websocket cannot communicate with the entire business cluster when establishing a connection with user.</p>

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

Expand All @@ -16,7 +16,7 @@
<dependency>
<groupId>plus.jdk</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
</dependency>
```

Expand Down Expand Up @@ -67,6 +67,9 @@ plus.jdk.websocket.log-level=debug
# udp broadcast listening port, if it is less than or equal to 0, it will not listen for messages
plus.jdk.websocket.broadcast-monitor-port=10300
# Whether to print the received UDP broadcast information to the log
plus.jdk.websocket.print-broadcast-message=true
```

## Example of use
Expand All @@ -77,8 +80,8 @@ plus.jdk.websocket.broadcast-monitor-port=10300

It is enough to implement the `IWsSession` interface for session authentication.

This interface encapsulates a `userId`, the parameter user can customize its type. There is also a channel,
and subsequent user interactions with the object depend on this object
This interface encapsulates a `userId`, the parameter user can customize its type. There is also a channel
which is the connection between the current machine and the user

```java
package plus.jdk.broadcast.test.session;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>plus.jdk</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>

<name>spring-boot-starter-websocket</name>
<description>A simple websocket component base on netty </description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected void sendBroadcast(Object userId, String path, byte[] data, Monitor[]
builder.setData(ByteString.copyFrom(data));
builder.setUid(userId == null ? "" : userId.toString());
builder.setPath(path);
builder.setType(messageType);
WsMessage wsMessage = builder.build();
udpMessageBroadcaster.publish(new BroadcastMessage(wsMessage.toByteArray(), Arrays.asList(monitors)));
}
Expand All @@ -77,6 +78,9 @@ public void run(ApplicationArguments args) throws Exception {
WsMessage wsMessage = WsMessage.parseFrom(msg.getContent());
SessionGroupManager sessionGroupManager = beanFactory.getBean(SessionGroupManager.class);
ConcurrentLinkedDeque<IWsSession<?>> sessions = sessionGroupManager.getSession(wsMessage.getUid(), wsMessage.getPath());
if(properties.getPrintBroadcastMessage()) {
log.info("receive broadcast message: {}", wsMessage);
}
for (IWsSession<?> session : sessions) {
if(MessageType.MESSAGE_TYPE_TEXT.equals(wsMessage.getType())) {
session.sendText(new String(wsMessage.getData().toByteArray()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public class WebsocketProperties {
*/
private Integer broadcastMonitorPort = 10300;

/**
* 是否将接收到的udp广播内容打印到日志中
*/
private Boolean printBroadcastMessage = true;

/**
* 认证器
Expand Down

0 comments on commit f29f1de

Please sign in to comment.