Skip to content

Commit e282780

Browse files
committed
init
0 parents  commit e282780

File tree

12 files changed

+385
-0
lines changed

12 files changed

+385
-0
lines changed

.gitattributes

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
### 设置项目语言 ###
2+
*.js linguist-language=java
3+
*.css linguist-language=java
4+
*.html linguist-language=java

.gitignore

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/application.pid
27+
/application.port

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# spring-boot-websocket-demo
2+
![](https://img.shields.io/static/v1?label=java&message=1.8&color=blue)
3+
![](https://img.shields.io/static/v1?label=spring-boot&message=2.4.5.RELEASE&color=blue)
4+
![](https://img.shields.io/static/v1?label=junit&message=4.13.2&color=black)
5+
6+
* https://docs.spring.io/spring-framework/docs/5.3.4/reference/html/web.html#websocket

pom.xml

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>cn.netbuffer</groupId>
7+
<artifactId>spring-boot-websocket-demo</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-websocket-demo</name>
12+
<url>https://github.com/netbuffer/spring-boot-websocket-demo</url>
13+
<description>spring-boot websocket相关技术测试</description>
14+
15+
<repositories>
16+
<repository>
17+
<id>spring-snapshots</id>
18+
<name>Spring snapshots</name>
19+
<url>https://repo.spring.io/libs-release</url>
20+
<snapshots>
21+
<enabled>true</enabled>
22+
</snapshots>
23+
</repository>
24+
</repositories>
25+
26+
<developers>
27+
<developer>
28+
<name>netbuffer</name>
29+
<email>javawiki@163.com</email>
30+
<url>https://github.com/netbuffer</url>
31+
</developer>
32+
</developers>
33+
34+
<parent>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-parent</artifactId>
37+
<version>2.4.5</version>
38+
<relativePath/>
39+
</parent>
40+
41+
42+
<properties>
43+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
44+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
45+
<java.version>1.8</java.version>
46+
</properties>
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-actuator</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-starter-web</artifactId>
56+
<exclusions>
57+
<exclusion>
58+
<groupId>org.springframework.boot</groupId>
59+
<artifactId>spring-boot-starter-tomcat</artifactId>
60+
</exclusion>
61+
</exclusions>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-starter-undertow</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-starter-websocket</artifactId>
70+
</dependency>
71+
<dependency>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-devtools</artifactId>
74+
<scope>runtime</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.projectlombok</groupId>
78+
<artifactId>lombok</artifactId>
79+
<optional>true</optional>
80+
</dependency>
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-starter-test</artifactId>
84+
<scope>test</scope>
85+
</dependency>
86+
<dependency>
87+
<groupId>cn.netbuffer</groupId>
88+
<artifactId>print-server-address</artifactId>
89+
<version>2.1.0</version>
90+
</dependency>
91+
<dependency>
92+
<groupId>com.fasterxml.jackson.dataformat</groupId>
93+
<artifactId>jackson-dataformat-xml</artifactId>
94+
<version>2.10.3</version>
95+
</dependency>
96+
<dependency>
97+
<groupId>com.fasterxml.jackson.core</groupId>
98+
<artifactId>jackson-core</artifactId>
99+
<version>2.10.3</version>
100+
</dependency>
101+
<dependency>
102+
<groupId>junit</groupId>
103+
<artifactId>junit</artifactId>
104+
<version>4.13.2</version>
105+
<scope>test</scope>
106+
</dependency>
107+
</dependencies>
108+
109+
<build>
110+
<finalName>${project.name}</finalName>
111+
<resources>
112+
<resource>
113+
<directory>src/main/resources</directory>
114+
<filtering>true</filtering>
115+
</resource>
116+
</resources>
117+
<plugins>
118+
<plugin>
119+
<groupId>pl.project13.maven</groupId>
120+
<artifactId>git-commit-id-plugin</artifactId>
121+
</plugin>
122+
<plugin>
123+
<groupId>org.springframework.boot</groupId>
124+
<artifactId>spring-boot-maven-plugin</artifactId>
125+
</plugin>
126+
<plugin>
127+
<groupId>org.apache.maven.plugins</groupId>
128+
<artifactId>maven-help-plugin</artifactId>
129+
<version>3.1.1</version>
130+
</plugin>
131+
</plugins>
132+
</build>
133+
134+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.netbuffer.springboot.websocket.demo;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.scheduling.annotation.EnableAsync;
7+
import org.springframework.web.socket.config.annotation.EnableWebSocket;
8+
9+
@Slf4j
10+
@EnableAsync
11+
@EnableWebSocket
12+
@SpringBootApplication
13+
public class SpringBootWebsocketDemoApplication {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(SpringBootWebsocketDemoApplication.class, args);
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.netbuffer.springboot.websocket.demo.config;
2+
3+
import cn.netbuffer.springboot.websocket.demo.websocket.handler.TextHandler;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.socket.WebSocketHandler;
8+
import org.springframework.web.socket.WebSocketSession;
9+
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
10+
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
@Slf4j
16+
@Configuration
17+
public class SpringWebSocketConfig implements WebSocketConfigurer {
18+
19+
@Bean
20+
public Map<String, WebSocketSession> sessionContainer() {
21+
return new HashMap<>();
22+
}
23+
24+
@Override
25+
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
26+
registry.addHandler(buildTextHandler(), "/ws").setAllowedOrigins("*");
27+
}
28+
29+
@Bean
30+
public WebSocketHandler buildTextHandler() {
31+
return new TextHandler();
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.netbuffer.springboot.websocket.demo.controller;
2+
3+
import cn.netbuffer.springboot.websocket.demo.websocket.handler.SessionManager;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.web.socket.TextMessage;
9+
import org.springframework.web.socket.WebSocketMessage;
10+
11+
import java.io.IOException;
12+
13+
@RestController
14+
@RequestMapping("/ws")
15+
public class WebsocketController {
16+
17+
@GetMapping("{session}/send")
18+
public void send(@PathVariable("session") String session, String msg) {
19+
WebSocketMessage webSocketMessage = new TextMessage(msg);
20+
try {
21+
SessionManager.get(session).sendMessage(webSocketMessage);
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package cn.netbuffer.springboot.websocket.demo.websocket.handler;
2+
3+
import org.springframework.web.socket.WebSocketSession;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
public class SessionManager {
8+
9+
private static final Map<String, WebSocketSession> sessions = new HashMap<>();
10+
11+
private SessionManager() {
12+
}
13+
14+
public static WebSocketSession put(String key,WebSocketSession webSocketSession) {
15+
return sessions.put(key,webSocketSession);
16+
}
17+
18+
public static WebSocketSession get(String key) {
19+
return sessions.get(key);
20+
}
21+
22+
public static WebSocketSession remove(String key) {
23+
return sessions.remove(key);
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cn.netbuffer.springboot.websocket.demo.websocket.handler;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.web.socket.CloseStatus;
5+
import org.springframework.web.socket.TextMessage;
6+
import org.springframework.web.socket.WebSocketSession;
7+
import org.springframework.web.socket.handler.TextWebSocketHandler;
8+
9+
@Slf4j
10+
public class TextHandler extends TextWebSocketHandler {
11+
12+
@Override
13+
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
14+
if (session.isOpen()) {
15+
log.info("receive websocket message:{} session:{}", message, session.getId());
16+
TextMessage returnMessage = new TextMessage(message.getPayload() + " received at server");
17+
session.sendMessage(returnMessage);
18+
}
19+
}
20+
21+
@Override
22+
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
23+
super.afterConnectionEstablished(session);
24+
SessionManager.put(session.getId(), session);
25+
log.info("session[{}] afterConnectionEstablished", session.getId());
26+
}
27+
28+
@Override
29+
public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
30+
log.info("session[{}] afterConnectionClosed", session.getId());
31+
SessionManager.remove(session.getId());
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
org.springframework.context.ApplicationListener=\
2+
org.springframework.boot.context.ApplicationPidFileWriter,\
3+
org.springframework.boot.web.context.WebServerPortFileWriter

src/main/resources/application.yaml

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
server:
2+
port: 17000
3+
servlet:
4+
session:
5+
timeout: 30m
6+
# 开启优雅停机
7+
shutdown: graceful
8+
http2:
9+
enabled: true
10+
11+
spring:
12+
lifecycle:
13+
timeout-per-shutdown-phase: 30s
14+
task:
15+
execution:
16+
pool:
17+
core-size: 4
18+
queue-capacity: 20
19+
max-size: 10
20+
keep-alive: "30s"
21+
application:
22+
name: spring-boot-websocket-demo
23+
# 在actuator监控中,当属性名中包含:password、secret、key这些敏感词,在返回它们的时候会使用*来掩码
24+
key: test-actuator
25+
26+
management:
27+
info:
28+
git:
29+
mode: full
30+
health:
31+
defaults:
32+
enabled: true
33+
endpoints:
34+
web:
35+
exposure:
36+
# 开启actuator所有监控端点
37+
include: "*"
38+
enabled-by-default: true
39+
endpoint:
40+
health:
41+
show-details: always
42+
43+
logging:
44+
level:
45+
root: WARN
46+
web: debug
47+
org:
48+
springframework:
49+
web: info
50+
cn:
51+
netbuffer: DEBUG
52+
info:
53+
app:
54+
name: test-info-api
55+
56+
# 输出更多日志信息(embedded container, Hibernate, and Spring Boot)
57+
debug: false

0 commit comments

Comments
 (0)