首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用Spring5的反应式WebSockets -如何获取初始消息

使用Spring5的反应式WebSockets -如何获取初始消息
EN

Stack Overflow用户
提问于 2018-02-13 17:12:59
回答 1查看 1.6K关注 0票数 4

我遵循了该教程(特别是关于浏览器WebSocket客户端的部分):http://www.baeldung.com/spring-5-reactive-websockets一切都很好。

我想更进一步,让我的处理程序根据从客户端发送的参数进行操作。在连接时,客户端发送一条消息("event-me-from-browser"):

代码语言:javascript
复制
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)检索该消息:

代码语言:javascript
复制
@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());
}

但它不起作用。

有什么想法吗?我做错什么了?

EN

回答 1

Stack Overflow用户

发布于 2018-08-24 07:04:03

排除tomcat依赖项(并使用netty )将会起作用:

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48763126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档