ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何使用Spring 5 Reactive WebSocket检测断开连接的客户端

2019-11-11 06:10:38  阅读:567  来源: 互联网

标签:spring-webflux spring-websocket websocket spring java


我设法使用Spring 5 Reactive WebSocket支持(Chapter 23.2.4)创建了一个WebSocketHandler.接收和发送一切正常.但是,我不知道如何检测客户端断开连接.调试客户端断开连接时,它会在HttpServerWSOperations类(netty.http.server包)的服务器端停止,并在其中检测到CloseWebSocketFrame.

任何建议如何处理客户端断开连接?

解决方法:

我在反应式org.springframework.web.reactive.socket.WebSocketHandler中实现了关闭事件处理程序,如下所示:

public Mono<Void> handle(final WebSocketSession session) {
    final String sessionId = session.getId();
    if(sessions.add(sessionId)) {  // add session id to set to keep a count of active sessions
        LOG.info("Starting WebSocket Session [{}]", sessionId);
        // Send the session id back to the client
        WebSocketMessage msg = session.textMessage(String.format("{\"session\":\"%s\"}", sessionId));
        // Register the outbound flux as the source of outbound messages
        final Flux<WebSocketMessage> outFlux = Flux.concat(Flux.just(msg), newMetricFlux.map(metric -> {
            LOG.info("Sending message to client [{}]: {}", sessionId, metric);
            return session.textMessage(metric);             
        }));
        // Subscribe to the inbound message flux
        session.receive().doFinally(sig -> {
            LOG.info("Terminating WebSocket Session (client side) sig: [{}], [{}]", sig.name(), sessionId);
            session.close();
            sessions.remove(sessionId);  // remove the stored session id
        }).subscribe(inMsg -> {
            LOG.info("Received inbound message from client [{}]: {}", sessionId, inMsg.getPayloadAsText());
        });
        return session.send(outFlux);
    }
    return Mono.empty();
}

newMetricFlux字段是出站Websocket消息的来源.挂钩关闭事件的诀窍是入站消息流上的doFinally.当websocket客户端关闭时,入站流量终止.

但是由于某种原因,从关闭网络通道到执行doFinally回调之间有1分钟的延迟.不知道为什么.

这是浏览器客户端连接并立即关闭的日志输出.注意第3行和第4行之间的60秒延迟.

2017-08-03 11:15:41.177 DEBUG 28505 --- [ctor-http-nio-2] r.i.n.http.server.HttpServerOperations   : New http connection, requesting read
2017-08-03 11:15:41.294  INFO 28505 --- [ctor-http-nio-2] c.h.w.ws.NewMetricsWebSocketHandler      : Starting WebSocket Session [87fbe66]
2017-08-03 11:15:48.294 DEBUG 28505 --- [ctor-http-nio-2] r.i.n.http.server.HttpServerOperations   : CloseWebSocketFrame detected. Closing Websocket
2017-08-03 11:16:48.293  INFO 28505 --- [ctor-http-nio-2] c.h.w.ws.NewMetricsWebSocketHandler      : Terminating WebSocket Session (client side) sig: [ON_COMPLETE], [87fbe66]

更新:2017年10月13日:

从Spring 5 GA开始,上述延迟不存在,我观察到在关闭客户端后立即调用了我的回调.不确定此版本已修复,但正如我所说,它已在5.0 GA中修复.

标签:spring-webflux,spring-websocket,websocket,spring,java
来源: https://codeday.me/bug/20191111/2018049.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有