Netty: properly closing WebSockets

How can I close the WebSocket channel / connection from the server side correctly? If I use ctx.getChannel().close() , then onerror in Brwoser (Firefox 9) is raised:

Connection to ws: // localhost: 8080 / websocket was interrupted during page load

I also tried sending the CloseWebSocketFrame to the channelClosed method in the WebSocketServerHandler :

 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { CloseWebSocketFrame close = new CloseWebSocketFrame(); ctx.getChannel().write(close); } 

This raises a ClosedChannelException (perhaps related to this ?).

+4
source share
3 answers

You must do this:

 ch.write(new CloseWebSocketFrame()); 

then the server will close the connection. If the connection does not close fast enough, you can call ch.close() .

+4
source

What about

 ctx.getChannel().write(new CloseWebSocketFrame()).addListener(ChannelFutureListener.CLOSE); 

This should send a CloseWebSocketFrame, and then close the channel after that.

+1
source

Which version are you using?

You tried to do this:

 ctx.getChannel().write(ChannelBuffers.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); 

?

0
source

Source: https://habr.com/ru/post/1392500/


All Articles