我遵循了该教程(特别是关于浏览器WebSocket客户端的部分):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());
}
但它不起作用。
有什么想法吗?我做错什么了?
发布于 2018-08-24 07:04:03
排除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>
https://stackoverflow.com/questions/48763126
复制相似问题