Skip to content

Commit 829ef3c

Browse files
committed
up
1 parent dc18574 commit 829ef3c

26 files changed

+674
-605
lines changed

hello-websocket-spring-boot/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33

44
# Hello Websocket java(spring-boot)
55

6+
## API
7+
8+
- WebSocket
9+
- "/websocket/text"
10+
- "/websocket/binary"
11+
- WebSocket SockJs
12+
- "/websocket-sockjs"
13+
- WebSocket Stomp
14+
- "/websocket-sockjs-stomp"
15+
- Broker "/queue", "/topic"
16+
- DestinationPrefixes "/hello"
17+
618
## tomcat-embed & websocket
719

820
| spring-boot-starter-tomcat | spring-cloud | spring-core | tomcat-embed-core | Servlet Spec | WebSocket Spec | JDK |

hello-websocket-spring-boot/client/pom.xml

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
54
<modelVersion>4.0.0</modelVersion>
65

76
<groupId>org.feuyeux.websocket</groupId>
@@ -16,6 +15,8 @@
1615
<maven.compiler.source>21</maven.compiler.source>
1716
<maven.compiler.target>21</maven.compiler.target>
1817
<springboot.version>3.2.3</springboot.version>
18+
<!-- https://mvnrepository.com/artifact/com.coveo/fmt-maven-plugin -->
19+
<fmt-maven-plugin.version>2.23</fmt-maven-plugin.version>
1920
</properties>
2021

2122
<dependencies>
@@ -44,6 +45,19 @@
4445

4546
<build>
4647
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.11.0</version>
52+
<configuration>
53+
<encoding>UTF-8</encoding>
54+
<release>21</release>
55+
<compilerArgs>
56+
<arg>-Xlint:unchecked</arg>
57+
<arg>-Xlint:-options</arg>
58+
</compilerArgs>
59+
</configuration>
60+
</plugin>
4761
<plugin>
4862
<groupId>org.springframework.boot</groupId>
4963
<artifactId>spring-boot-maven-plugin</artifactId>
@@ -56,6 +70,23 @@
5670
</execution>
5771
</executions>
5872
</plugin>
73+
<plugin>
74+
<groupId>com.spotify.fmt</groupId>
75+
<artifactId>fmt-maven-plugin</artifactId>
76+
<version>${fmt-maven-plugin.version}</version>
77+
<configuration>
78+
<skipTestSourceDirectory>true</skipTestSourceDirectory>
79+
<skipSortingImports>false</skipSortingImports>
80+
<style>google</style>
81+
</configuration>
82+
<executions>
83+
<execution>
84+
<goals>
85+
<goal>format</goal>
86+
</goals>
87+
</execution>
88+
</executions>
89+
</plugin>
5990
</plugins>
6091
</build>
6192
</project>

hello-websocket-spring-boot/client/src/main/java/org/feuyeux/websocket/ClientWsApplication.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66

77
@SpringBootApplication
88
public class ClientWsApplication {
9-
public static void main(String[] args) {
10-
new SpringApplicationBuilder(ClientWsApplication.class)
11-
.web(WebApplicationType.NONE)
12-
.run(args);
13-
}
9+
public static void main(String[] args) {
10+
new SpringApplicationBuilder(ClientWsApplication.class).web(WebApplicationType.NONE).run(args);
11+
}
1412
}

hello-websocket-spring-boot/client/src/main/java/org/feuyeux/websocket/codec/EchoRequestCodec.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@
66

77
public class EchoRequestCodec {
88

9-
public static ByteBuf encode(EchoRequest echoRequest) {
10-
ByteBuf byteBuf = Unpooled.buffer();
11-
byteBuf.writeLong(echoRequest.getId());
12-
byteBuf.writeBytes(echoRequest.getData().getBytes());
13-
return byteBuf;
14-
}
9+
public static ByteBuf encode(EchoRequest echoRequest) {
10+
ByteBuf byteBuf = Unpooled.buffer();
11+
byteBuf.writeLong(echoRequest.getId());
12+
byteBuf.writeBytes(echoRequest.getData().getBytes());
13+
return byteBuf;
14+
}
1515

16-
public static EchoRequest decode(ByteBuf byteBuf) {
17-
long id = byteBuf.readLong();
18-
int currentIndex = byteBuf.readerIndex();
19-
int endIndex = byteBuf.writerIndex();
16+
public static EchoRequest decode(ByteBuf byteBuf) {
17+
long id = byteBuf.readLong();
18+
int currentIndex = byteBuf.readerIndex();
19+
int endIndex = byteBuf.writerIndex();
2020

21-
byte[] dst = new byte[endIndex - currentIndex];
22-
byteBuf.readBytes(dst);
23-
String data = new String(dst);
24-
return new EchoRequest(id, data);
25-
}
21+
byte[] dst = new byte[endIndex - currentIndex];
22+
byteBuf.readBytes(dst);
23+
String data = new String(dst);
24+
return new EchoRequest(id, data);
25+
}
2626
}

hello-websocket-spring-boot/client/src/main/java/org/feuyeux/websocket/codec/EchoResponseCodec.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66

77
public class EchoResponseCodec {
88

9-
public static ByteBuf encode(EchoResponse echoResponse) {
10-
ByteBuf byteBuf = Unpooled.buffer();
11-
byteBuf.writeLong(echoResponse.getId());
12-
byteBuf.writeLong(echoResponse.getElapse());
13-
byteBuf.writeBytes(echoResponse.getData().getBytes());
14-
return byteBuf;
15-
}
9+
public static ByteBuf encode(EchoResponse echoResponse) {
10+
ByteBuf byteBuf = Unpooled.buffer();
11+
byteBuf.writeLong(echoResponse.getId());
12+
byteBuf.writeLong(echoResponse.getElapse());
13+
byteBuf.writeBytes(echoResponse.getData().getBytes());
14+
return byteBuf;
15+
}
1616

17-
public static EchoResponse decode(ByteBuf byteBuf) {
18-
long id = byteBuf.readLong();
19-
long elapse = byteBuf.readLong();
20-
int currentIndex = byteBuf.readerIndex();
21-
int endIndex = byteBuf.writerIndex();
17+
public static EchoResponse decode(ByteBuf byteBuf) {
18+
long id = byteBuf.readLong();
19+
long elapse = byteBuf.readLong();
20+
int currentIndex = byteBuf.readerIndex();
21+
int endIndex = byteBuf.writerIndex();
2222

23-
byte[] dst = new byte[endIndex - currentIndex];
24-
byteBuf.readBytes(dst);
25-
String data = new String(dst);
26-
return new EchoResponse(id, elapse, data);
27-
}
23+
byte[] dst = new byte[endIndex - currentIndex];
24+
byteBuf.readBytes(dst);
25+
String data = new String(dst);
26+
return new EchoResponse(id, elapse, data);
27+
}
2828
}

hello-websocket-spring-boot/client/src/main/java/org/feuyeux/websocket/config/ClientWebSocketConfig.java

+25-25
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,29 @@
1010
@Configuration
1111
public class ClientWebSocketConfig {
1212

13-
@Bean
14-
public WebSocketConnectionManager textWebSocketConnectionManager() {
15-
ClientTextWebSocketHandler clientTextWebSocketHandler = new ClientTextWebSocketHandler();
16-
clientTextWebSocketHandler.setType("websocket-text");
17-
WebSocketConnectionManager manager = new WebSocketConnectionManager(
18-
new StandardWebSocketClient(),
19-
clientTextWebSocketHandler,
20-
"ws://localhost:8080/websocket/text"
21-
);
22-
manager.setAutoStartup(true);
23-
return manager;
24-
}
13+
@Bean
14+
public WebSocketConnectionManager textWebSocketConnectionManager() {
15+
ClientTextWebSocketHandler clientTextWebSocketHandler = new ClientTextWebSocketHandler();
16+
clientTextWebSocketHandler.setType("websocket-text");
17+
WebSocketConnectionManager manager =
18+
new WebSocketConnectionManager(
19+
new StandardWebSocketClient(),
20+
clientTextWebSocketHandler,
21+
"ws://localhost:8080/websocket/text");
22+
manager.setAutoStartup(true);
23+
return manager;
24+
}
2525

26-
@Bean
27-
public WebSocketConnectionManager binaryWebSocketConnectionManager() {
28-
ClientBinaryWebSocketHandler clientBinaryWebSocketHandler = new ClientBinaryWebSocketHandler();
29-
clientBinaryWebSocketHandler.setType("websocket-binary");
30-
WebSocketConnectionManager manager = new WebSocketConnectionManager(
31-
new StandardWebSocketClient(),
32-
clientBinaryWebSocketHandler,
33-
"ws://localhost:8080/websocket/binary"
34-
);
35-
manager.setAutoStartup(true);
36-
return manager;
37-
}
38-
}
26+
@Bean
27+
public WebSocketConnectionManager binaryWebSocketConnectionManager() {
28+
ClientBinaryWebSocketHandler clientBinaryWebSocketHandler = new ClientBinaryWebSocketHandler();
29+
clientBinaryWebSocketHandler.setType("websocket-binary");
30+
WebSocketConnectionManager manager =
31+
new WebSocketConnectionManager(
32+
new StandardWebSocketClient(),
33+
clientBinaryWebSocketHandler,
34+
"ws://localhost:8080/websocket/binary");
35+
manager.setAutoStartup(true);
36+
return manager;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.feuyeux.websocket.config;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import org.feuyeux.websocket.handler.ClientTextWebSocketHandler;
46
import org.springframework.context.annotation.Bean;
57
import org.springframework.context.annotation.Configuration;
@@ -12,34 +14,31 @@
1214
import org.springframework.web.socket.sockjs.client.Transport;
1315
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
1416

15-
import java.util.ArrayList;
16-
import java.util.List;
17-
1817
@Configuration
1918
public class ClientWebSocketSockJsConfig {
20-
@Bean
21-
public WebSocketConnectionManager webSocketSockJsConnectionManager() {
22-
WebSocketConnectionManager manager = new WebSocketConnectionManager(
23-
webSocketSockJsClient(),
24-
webSocketSockJsHandler(),
25-
"http://localhost:8080/websocket-sockjs"
26-
);
27-
manager.setAutoStartup(true);
28-
return manager;
29-
}
19+
@Bean
20+
public WebSocketConnectionManager webSocketSockJsConnectionManager() {
21+
WebSocketConnectionManager manager =
22+
new WebSocketConnectionManager(
23+
webSocketSockJsClient(),
24+
webSocketSockJsHandler(),
25+
"http://localhost:8080/websocket-sockjs");
26+
manager.setAutoStartup(true);
27+
return manager;
28+
}
3029

31-
@Bean
32-
public WebSocketClient webSocketSockJsClient() {
33-
List<Transport> transports = new ArrayList<>();
34-
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
35-
transports.add(new RestTemplateXhrTransport());
36-
return new SockJsClient(transports);
37-
}
30+
@Bean
31+
public WebSocketClient webSocketSockJsClient() {
32+
List<Transport> transports = new ArrayList<>();
33+
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
34+
transports.add(new RestTemplateXhrTransport());
35+
return new SockJsClient(transports);
36+
}
3837

39-
@Bean
40-
public WebSocketHandler webSocketSockJsHandler() {
41-
ClientTextWebSocketHandler clientTextWebSocketHandler = new ClientTextWebSocketHandler();
42-
clientTextWebSocketHandler.setType("websocket-sockjs");
43-
return clientTextWebSocketHandler;
44-
}
38+
@Bean
39+
public WebSocketHandler webSocketSockJsHandler() {
40+
ClientTextWebSocketHandler clientTextWebSocketHandler = new ClientTextWebSocketHandler();
41+
clientTextWebSocketHandler.setType("websocket-sockjs");
42+
return clientTextWebSocketHandler;
43+
}
4544
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.feuyeux.websocket.config;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
35
import org.feuyeux.websocket.handler.ClientStompSessionHandler;
46
import org.springframework.beans.factory.annotation.Qualifier;
57
import org.springframework.context.annotation.Bean;
@@ -14,30 +16,29 @@
1416
import org.springframework.web.socket.sockjs.client.Transport;
1517
import org.springframework.web.socket.sockjs.client.WebSocketTransport;
1618

17-
import java.util.ArrayList;
18-
import java.util.List;
19-
2019
@Configuration
2120
public class ClientWebSocketSockJsStompConfig {
22-
@Bean
23-
public WebSocketStompClient webSocketStompClient(@Qualifier("stompSessionClient") WebSocketClient stompSessionClient,
24-
StompSessionHandler stompSessionHandler) {
25-
WebSocketStompClient webSocketStompClient = new WebSocketStompClient(stompSessionClient);
26-
webSocketStompClient.setMessageConverter(new StringMessageConverter());
27-
webSocketStompClient.connectAsync("http://localhost:8080/websocket-sockjs-stomp", stompSessionHandler);
28-
return webSocketStompClient;
29-
}
21+
@Bean
22+
public WebSocketStompClient webSocketStompClient(
23+
@Qualifier("stompSessionClient") WebSocketClient stompSessionClient,
24+
StompSessionHandler stompSessionHandler) {
25+
WebSocketStompClient webSocketStompClient = new WebSocketStompClient(stompSessionClient);
26+
webSocketStompClient.setMessageConverter(new StringMessageConverter());
27+
webSocketStompClient.connectAsync(
28+
"http://localhost:8080/websocket-sockjs-stomp", stompSessionHandler);
29+
return webSocketStompClient;
30+
}
3031

31-
@Bean
32-
public WebSocketClient stompSessionClient() {
33-
List<Transport> transports = new ArrayList<>();
34-
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
35-
transports.add(new RestTemplateXhrTransport());
36-
return new SockJsClient(transports);
37-
}
32+
@Bean
33+
public WebSocketClient stompSessionClient() {
34+
List<Transport> transports = new ArrayList<>();
35+
transports.add(new WebSocketTransport(new StandardWebSocketClient()));
36+
transports.add(new RestTemplateXhrTransport());
37+
return new SockJsClient(transports);
38+
}
3839

39-
@Bean
40-
public StompSessionHandler stompSessionHandler() {
41-
return new ClientStompSessionHandler();
42-
}
40+
@Bean
41+
public StompSessionHandler stompSessionHandler() {
42+
return new ClientStompSessionHandler();
43+
}
4344
}

0 commit comments

Comments
 (0)