Tomasz Kryński Tomasz Kryński - 27 days ago 14
Android Question

Netty websocket close gracefully

I need to make my websocket to close gracefully. Firstly send all messages that are waiting in buffer then close.

channel.flush();
channel.close();


seems not right, because I dont have any completion handler for flush method to do close after it is done.

Is there any way to do this.
Netty used: 4.1.0.Beta3

Answer

The trick you can use is to writeAndFlush an empty buffer, then attach a listener to that call. Unpooled.EMPTY_BUFFER can make this pretty easy.

channel.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(...
    channel.close();
)};
Comments