Skip to content

Commit

Permalink
spring6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Dec 19, 2022
1 parent 656ec31 commit 08c2a71
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 91 deletions.
96 changes: 33 additions & 63 deletions example-spring6-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<!-- <version>2.7.1</version>--><!--spring5-->
<relativePath/>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-parent</artifactId>
<version>2.0.22-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.example</groupId>
<artifactId>spring6test</artifactId>
Expand All @@ -21,67 +20,75 @@
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
<spring.boot.version>3.0.0</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-spring6</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring.boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!--排除掉Jackson-->
<version>${spring.boot.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring.boot.version}</version>
<scope>runtime</scope>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${spring.boot.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2-extension-spring6</artifactId>
<version>${fastjson2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<configuration>
<excludes>
<exclude>
Expand All @@ -93,41 +100,4 @@
</plugin>
</plugins>
</build>
<!-- 使用 aliyun 的 Maven 源,提升下载速度 -->
<repositories>
<repository>
<id>aliyunmaven</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</repository>
<!--拉取springboot3时,需要注释掉阿里云仓库配置,目前阿里云仓库没有该依赖-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>snapshots</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyunmaven</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
@SpringBootApplication
@EnableWebSocket
public class Spring6testApplication {

public static void main(String[] args) {
SpringApplication.run(Spring6testApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,37 @@
* Created by jackiechan on 2018/2/5/下午4:11
* 文本消息的处理器
*/
public class ChatMessageHandler extends TextWebSocketHandler {

private static final Map<String, WebSocketSession> allClients;//用于缓存所有的用户和连接之间的关系
public class ChatMessageHandler
extends TextWebSocketHandler {
// 用于缓存所有的用户和连接之间的关系
private static final Map<String, WebSocketSession> allClients;

static {
allClients = new ConcurrentHashMap();//初始化连接
// 初始化连接
allClients = new ConcurrentHashMap();
}

/**
* 当和用户成功建立连接的时候会调用此方法,在此方法内部应该保存连接
*/
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {


}


/**
* 给某个用户发送消息
*
* @param userName
* @param message
*/
public void sendMessageToUser(String userName, TextMessage message) {
WebSocketSession webSocketSession = allClients.get(userName);//根据接收方的名字找到对应的连接
if (webSocketSession != null && webSocketSession.isOpen()) {//如果没有离线,如果离线,请根据实际业务需求来处理,可能会需要保存离线消息
//根据接收方的名字找到对应的连接
WebSocketSession webSocketSession = allClients.get(userName);
if (webSocketSession != null && webSocketSession.isOpen()) {
//如果没有离线,如果离线,请根据实际业务需求来处理,可能会需要保存离线消息
try {
webSocketSession.sendMessage(message);//发送消息
//发送消息
webSocketSession.sendMessage(message);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -53,9 +55,9 @@ public void sendMessageToUser(String userName, TextMessage message) {
* @param message
*/
public void sendMessageToUsers(TextMessage message) {
for (Map.Entry<String, WebSocketSession> webSocketSessionEntry : allClients.entrySet()) {//获取所有的连接

WebSocketSession session = webSocketSessionEntry.getValue();//找到每个连接
for (Map.Entry<String, WebSocketSession> webSocketSessionEntry : allClients.entrySet()) {
//获取所有的连接
WebSocketSession session = webSocketSessionEntry.getValue();
if (session != null && session.isOpen()) {
try {
session.sendMessage(message);
Expand All @@ -66,10 +68,8 @@ public void sendMessageToUsers(TextMessage message) {
}
}


@Override
public boolean supportsPartialMessages() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

@Configuration
@EnableWebMvc
public class WebMvcConfigurer implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {
public class WebMvcConfigurer
implements org.springframework.web.servlet.config.annotation.WebMvcConfigurer {
/**
* 与org.springframework.web.servlet.config.annotation.WebMvcConfigurer.configureMessageConverters()方法的区别是:
* 使用configureMessageConverters方法会导致springboot不会注入默认的消息转换器
Expand Down Expand Up @@ -49,5 +50,4 @@ public void configureViewResolvers(ViewResolverRegistry registry) {
//fastJsonJsonView.setFastJsonConfig(config);
registry.enableContentNegotiation(fastJsonJsonView);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

public class WebSocketConfig implements WebSocketConfigurer {
public class WebSocketConfig
implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
System.out.println("初始化路径拦截");
webSocketHandlerRegistry.addHandler(new ChatMessageHandler(), "/websocket/*").withSockJS().setMessageCodec(new FastjsonSockJsMessageCodec());
}

}

13 changes: 7 additions & 6 deletions extension-spring6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<url>https://github.com/alibaba/fastjson2</url>
<connection>scm:git:https://git@github.com/alibaba/fastjson2.git</connection>
</scm>

<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>

<organization>
<name>Alibaba Group</name>
<url>https://github.com/alibaba</url>
Expand Down Expand Up @@ -78,12 +85,6 @@
</developer>
</developers>

<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>${maven.compiler.source}</maven.compiler.target>
</properties>

<dependencyManagement>
<dependencies>
<!-- spring libs -->
Expand Down

0 comments on commit 08c2a71

Please sign in to comment.