Permalink
Browse files

Close the channel after the response was written to the client on a W…

…ebSocketHandshakeException. See #587
  • Loading branch information...
1 parent 6c2053b commit 0233a3dd631284cfbc10786c96c1cfa63316b5b3 @normanmaurer normanmaurer committed Sep 10, 2012
View
13 .../src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java
@@ -17,6 +17,7 @@
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
@@ -79,13 +80,11 @@ public void messageReceived(ChannelHandlerContext ctx, WebSocketFrame frame) thr
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- try {
- if (cause instanceof WebSocketHandshakeException) {
- DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
- response.setContent(Unpooled.wrappedBuffer(cause.getMessage().getBytes()));
- ctx.channel().write(response);
- }
- } finally {
+ if (cause instanceof WebSocketHandshakeException) {
+ DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST);
+ response.setContent(Unpooled.wrappedBuffer(cause.getMessage().getBytes()));
+ ctx.channel().write(response).addListener(ChannelFutureListener.CLOSE);
+ } else {
ctx.close();
}
}

0 comments on commit 0233a3d

Please sign in to comment.