SpringWebFlux基础教程:WebSocket使⽤
WebSocket 协议简介
WebSocket 协议提供了⼀种标准化的⽅式,在客户端和服务端建⽴在⼀个TCP 连接之上的全双⼯,双向通信的协议。
WebSocket 交互开始于 HTTP 请求,使⽤ HTTP 请求的 header 中的 Upgrade 进⾏切换到 WebSocket 协议。
HTTP 和 WebSocket 对⽐
即使 WebSocket 的设计兼容 HTTP 协议和开始于 HTTP 请求,了解两个协议是不同架构和应⽤模型是⾮常重要的。
在 HTTP 和 REST,⼀个应⽤有多个URL,对于交互的应⽤,客户端可以访问这些 URL,请求和响应的风格。服务端会根据 URL、http method、header 等等信息进⾏路由处
理。
相⽐之下, WebSocket 协议是使⽤⼀个URL 初始化连接。应⽤所有的消息通过同⼀个 TCP 通信,这⼀点是完全不同的异步、事件驱动的消息传递体系结构。
WebSocket API 的使⽤
1. Webflux 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
2. 定义⼀个Handler,⽤于处理数据的请求和响应
import java.time.Duration;
import org.active.socket.WebSocketHandler;前端websocket怎么用
import org.active.socket.WebSocketMessage;
import org.active.socket.WebSocketSession;
publisher.Flux;
publisher.Mono;
public class MyWebSocketHandler implements WebSocketHandler {
// 每间隔 1 秒,数字加 1
private Flux<Long> intervalFlux = Flux.interval(Duration.ofSeconds(1L), Duration.ofSeconds(1L));
@Override
public Mono<Void> handle(WebSocketSession session) {
return session.send(intervalFlux.map(item -> Message(item + "")))
.ive().map(WebSocketMessage::getPayloadAsText).log());
}
}
3. 配置类,配置 WebSocket 的 URL 信息
@Configuration
public class WebConfig {
@Bean
public WebSocketHandlerAdapter handlerAdapter() {
return new WebSocketHandlerAdapter();
}
@Bean
public HandlerMapping handlerMapping() {
Map<String, WebSocketHandler> urlMap = new HashMap<>();
urlMap.put("/path", new MyWebSocketHandler());
int order = -1;
return new SimpleUrlHandlerMapping(urlMap, order);
}
}
4. 客户端
import java.URI;
import java.URISyntaxException;
import java.time.Duration;
import org.active.socket.WebSocketMessage;
import org.active.socket.client.ReactorNettyWebSocketClient;
import org.active.socket.client.WebSocketClient;
publisher.Mono;
public class Client {
public static void main(String[] args) throws URISyntaxException {
WebSocketClient client = new ReactorNettyWebSocketClient();
URI url = new URI("ws://localhost:8080/path");
session -> session.send(Mono.Message("hello world")))
.ive().map(WebSocketMessage::getPayloadAsText).log())
.then())
.block(Duration.ofSeconds(10));
}
}
服务端⽇志
2021-08-10 22:42:36.162 INFO 85792 --- [ main] o.s.beddedty.NettyWebServer : Netty started on port(s): 8080
2021-08-10 22:42:36.168 INFO 85792 --- [ main] c.e.s.SpringBootDemoApplication : Started SpringBootDemoApplication in 3.583 seconds (JVM running for 4.462)
2021-08-10 22:42:51.518 INFO 85792 --- [ctor-http-nio-2] reactor.Flux.Map.1 : onSubscribe(FluxMap.MapSubscriber)
2021-08-10 22:42:51.522 INFO 85792 --- [ctor-http-nio-2] reactor.Flux.Map.1 : request(unbounded)
2021-08-10 22:42:51.534 INFO 85792 --- [ctor-http-nio-2] reactor.Flux.Map.1 : onNext(hello world)
2021-08-10 22:43:00.956 INFO 85792 --- [ctor-http-nio-2] reactor.Flux.Map.1 : onComplete()
客户端⽇志
22:42:51.527 [reactor-http-nio-1] DEBUG reactorty.channel.FluxReceive - [id: 0xd95be56c, L:/127.0.0.1:50773 - R:localhost/127.0.0.1:8080] Subscribing inbound receiver [pending: 0, cancelled:false, inboundDone: false] 22:42:51.530 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onSubscribe(FluxMap.MapSubscriber)
22:42:51.531 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - request(unbounded)
22:42:52.518 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(0)
22:42:53.513 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(1)
22:42:54.518 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(2)
22:42:55.513 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(3)
22:42:56.514 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(4)
22:42:57.516 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(5)
22:42:58.518 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(6)
22:42:59.512 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(7)
22:43:00.513 [reactor-http-nio-1] INFO reactor.Flux.Map.1 - onNext(8)
22:43:00.947 [main] INFO reactor.Flux.Map.1 - cancel()
22:43:00.948 [reactor-http-nio-1] DEBUG reactorty.http.client.HttpClientOperations - [id: 0xd95be56c, L:/127.0.0.1:50773 - R:localhost/127.0.0.1:8080] Cancelling Websocket inbound. Closing Websocket Exception in thread "main" java.lang.IllegalStateException: Timeout on blocking read for 10000 MILLISECONDS
⾄此,Spring WebFlux 中 WebSocket 协议使⽤的结束了。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论