我遵循了该教程(特别是浏览器 WebSocket 客户端的部分):http://www.baeldung.com/spring-5-reactive-websockets http://www.baeldung.com/spring-5-reactive-websockets一切正常。
我想更进一步,让我的处理程序根据客户端发送的参数进行操作。连接时,客户端发送一条消息(“event-me-from-browser”):
var clientWebSocket = new WebSocket("ws://localhost:8080/event-emitter");
clientWebSocket.onopen = function() {
console.log("clientWebSocket.onopen", clientWebSocket);
console.log("clientWebSocket.readyState", "websocketstatus");
clientWebSocket.send("event-me-from-browser");
}
我尝试在服务器端(java)检索该消息:
@Override
public Mono<Void> handle(WebSocketSession webSocketSession) {
webSocketSession.receive().handle((pWebSocketMessage, pSynchronousSink) -> System.out.println(pWebSocketMessage.getPayloadAsText()));
return webSocketSession.send(Flux.fromStream(Stream.generate(() -> getData()))
.delayElements(Duration.ofMillis(50))
.map(webSocketSession::textMessage))
.and(webSocketSession.receive()
.map(WebSocketMessage::getPayloadAsText)
.log());
}
但这不起作用。
任何想法?我做错了什么?
排除 tomcat 依赖(并使用 netty 代替)将起作用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- Exclude the Tomcat dependency -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)