File tree Expand file tree Collapse file tree 7 files changed +165
-0
lines changed
springboot-webflux-8-websocket
java/org/spring/springboot Expand file tree Collapse file tree 7 files changed +165
-0
lines changed Original file line number Diff line number Diff line change
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 >springboot</groupId >
7
+ <artifactId >springboot-webflux-8-websocket</artifactId >
8
+ <version >0.0.1-SNAPSHOT</version >
9
+ <name >springboot-webflux-8-websocket :: Spring Boot WebFlux 中 WebSocket 实现通信</name >
10
+
11
+ <parent >
12
+ <groupId >org.springframework.boot</groupId >
13
+ <artifactId >spring-boot-starter-parent</artifactId >
14
+ <version >2.0.1.RELEASE</version >
15
+ <relativePath /> <!-- lookup parent from repository -->
16
+ </parent >
17
+
18
+ <properties >
19
+ <project .build.sourceEncoding>UTF-8</project .build.sourceEncoding>
20
+ <project .reporting.outputEncoding>UTF-8</project .reporting.outputEncoding>
21
+ <java .version>1.8</java .version>
22
+ </properties >
23
+
24
+ <dependencies >
25
+ <dependency >
26
+ <groupId >org.springframework.boot</groupId >
27
+ <artifactId >spring-boot-starter-webflux</artifactId >
28
+ </dependency >
29
+
30
+ <dependency >
31
+ <groupId >org.springframework.boot</groupId >
32
+ <artifactId >spring-boot-starter-test</artifactId >
33
+ <scope >test</scope >
34
+ </dependency >
35
+ </dependencies >
36
+
37
+ <build >
38
+ <plugins >
39
+ <plugin >
40
+ <groupId >org.springframework.boot</groupId >
41
+ <artifactId >spring-boot-maven-plugin</artifactId >
42
+ </plugin >
43
+ </plugins >
44
+ </build >
45
+ </project >
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot ;
2
+
3
+ import org .springframework .boot .SpringApplication ;
4
+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5
+
6
+ /**
7
+ * Spring Boot 应用启动类
8
+ *
9
+ * Created by bysocket on 09/29/2017.
10
+ */
11
+ // Spring Boot 应用的标识
12
+ @ SpringBootApplication
13
+ public class Application {
14
+
15
+ public static void main (String [] args ) {
16
+ // 程序启动入口
17
+ // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18
+ SpringApplication .run (Application .class ,args );
19
+
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .config ;
2
+
3
+ import org .spring .springboot .handler .EchoHandler ;
4
+ import org .springframework .beans .factory .annotation .Autowired ;
5
+ import org .springframework .context .annotation .Bean ;
6
+ import org .springframework .context .annotation .Configuration ;
7
+ import org .springframework .core .Ordered ;
8
+ import org .springframework .web .reactive .HandlerMapping ;
9
+ import org .springframework .web .reactive .handler .SimpleUrlHandlerMapping ;
10
+ import org .springframework .web .reactive .socket .WebSocketHandler ;
11
+ import org .springframework .web .reactive .socket .server .support .WebSocketHandlerAdapter ;
12
+
13
+ import java .util .HashMap ;
14
+ import java .util .Map ;
15
+
16
+ @ Configuration
17
+ public class WebSocketConfiguration {
18
+
19
+ @ Autowired
20
+ @ Bean
21
+ public HandlerMapping webSocketMapping (final EchoHandler echoHandler ) {
22
+ final Map <String , WebSocketHandler > map = new HashMap <>();
23
+ map .put ("/echo" , echoHandler );
24
+
25
+ final SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping ();
26
+ mapping .setOrder (Ordered .HIGHEST_PRECEDENCE );
27
+ mapping .setUrlMap (map );
28
+ return mapping ;
29
+ }
30
+
31
+ @ Bean
32
+ public WebSocketHandlerAdapter handlerAdapter () {
33
+ return new WebSocketHandlerAdapter ();
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+
2
+ package org .spring .springboot .handler ;
3
+
4
+ import org .springframework .stereotype .Component ;
5
+ import org .springframework .web .reactive .socket .WebSocketHandler ;
6
+ import org .springframework .web .reactive .socket .WebSocketSession ;
7
+ import reactor .core .publisher .Mono ;
8
+
9
+ @ Component
10
+ public class EchoHandler implements WebSocketHandler {
11
+ @ Override
12
+ public Mono <Void > handle (final WebSocketSession session ) {
13
+ return session .send (
14
+ session .receive ()
15
+ .map (msg -> session .textMessage ("服务端返回:小明, -> " + msg .getPayloadAsText ())));
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 ">
5
+ < title > Client WebSocket</ title >
6
+ </ head >
7
+ < body >
8
+
9
+ < div class ="events "> </ div >
10
+ < script >
11
+ var clientWebSocket = new WebSocket ( "ws://localhost:8080/echo" ) ;
12
+ clientWebSocket . onopen = function ( ) {
13
+ console . log ( "clientWebSocket.onopen" , clientWebSocket ) ;
14
+ console . log ( "clientWebSocket.readyState" , "websocketstatus" ) ;
15
+ clientWebSocket . send ( "你好" ) ;
16
+ }
17
+
18
+ clientWebSocket . onclose = function ( error ) {
19
+ console . log ( "clientWebSocket.onclose" , clientWebSocket , error ) ;
20
+ events ( "再见" ) ;
21
+ }
22
+
23
+ function events ( responseEvent ) {
24
+ document . querySelector ( ".events" ) . innerHTML += responseEvent + "<br>" ;
25
+ }
26
+ </ script >
27
+ </ body >
28
+ </ html >
Original file line number Diff line number Diff line change
1
+ import org .springframework .web .reactive .socket .WebSocketMessage ;
2
+ import org .springframework .web .reactive .socket .client .ReactorNettyWebSocketClient ;
3
+ import org .springframework .web .reactive .socket .client .WebSocketClient ;
4
+ import reactor .core .publisher .Flux ;
5
+
6
+ import java .net .URI ;
7
+ import java .time .Duration ;
8
+
9
+ public class WSClient {
10
+ public static void main (final String [] args ) {
11
+ final WebSocketClient client = new ReactorNettyWebSocketClient ();
12
+ client .execute (URI .create ("ws://localhost:8080/echo" ), session ->
13
+ session .send (Flux .just (session .textMessage ("你好" )))
14
+ .thenMany (session .receive ().take (1 ).map (WebSocketMessage ::getPayloadAsText ))
15
+ .doOnNext (System .out ::println )
16
+ .then ())
17
+ .block (Duration .ofMillis (5000 ));
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments