繁体   English   中英

客户端断电或Netty断开Internet连接时如何检测客户端在服务器上的断开连接

[英]How to detect client's disconnect at server when client power off or lost internet connection in netty

我想在客户端关闭电源或失去互联网连接并从列表客户端中删除时检测客户端在服务器上的断开连接。 我可以做吗 ?

我正在尝试解决某些问题,但无法正常工作。 这是我的代码:

final ChannelFuture cf = chc.channel().writeAndFlush(new TextWebSocketFrame(dataSend));
            cf.addListener(new GenericFutureListener<Future<? super Void>>() {

                public void operationComplete(Future<? super Void> f) throws Exception {

                    if (!cf.isSuccess()) {

                        ServerHandler.agentChannelList.get(tranfer.getUsername()).remove(chc);
                        if (ServerHandler.agentChannelList.get(tranfer.getUsername()).isEmpty()) {
                            if (ServerHandler.agentOnlineQueue.containsKey(tranfer.getPage_id())) {
                                ServerHandler.agentOnlineQueue.get(tranfer.getPage_id()).remove(tranfer.getUsername());
                            }
                        }
                    }
                }

            });

唯一的“真实”解决方案是在协议中实现类似“乒乓”的功能。 基本上,您写入远程对等方,如果写入失败,或者远程对等方不响应,您将认为它已死。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM