Netty Channel关闭检测 [英] Netty Channel closed detection

查看:1146
本文介绍了Netty Channel关闭检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用netty和ios构建服务器客户端应用程序,当用户在他/她的ios设备上关闭WiFi时,我遇到了问题,netty服务器不知道它。
服务器需要知道为该用户进行清理并将他/她设置为脱机,但现在当用户再次尝试连接时,服务器只是告诉他他/她已经在线。

I am building a server client application using netty and ios, I am facing a problem when the user just turns off WiFi on his/her ios device, the netty server does not know about it. The server needs to know to do cleanup for that user and set him/her offline, but now when the user tries to connect again, the server just tells him that he/she is already online.

推荐答案

如果我理解你的问题:你想在服务器端侦听客户端频道关闭事件并做一些会话清理,

If I understood your problem correctly: You want to listen for client channel closed events in server side and do some session cleanup,

在Netty中有两种方法可以监听频道关闭事件:

There are two ways to listen for channel closed events in Netty :

1)如果服务器处理程序扩展 SimpleChannelHandler / SimpleChannelHandler ,然后你可以覆盖以下方法并在那里写你的会话清理逻辑

1) If your server handler extends SimpleChannelHandler/SimpleChannelHandler, then you can override following method and write your session cleanup logic there

public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception;

2)如果您只能访问频道参考,那么您可以获得频道关闭的未来和使用会话清理逻辑注册 ChannelFutureListener 的实现,

2) If you have only access to the channel reference, then you can get the channel close future and register your implementation of ChannelFutureListener with your session cleanup logic,

ChannelFuture closeFuture = channel.closeFuture();

closeFuture.addListener(new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) throws Exception {
        //session cleanup logic
    }
});

这篇关于Netty Channel关闭检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆