Merge pull request #115 from ngocdaothanh/master

Use channel instead of context and cleanup unused method argument
This commit is contained in:
Vibul Imtarnasan 2011-12-11 02:07:22 -08:00
commit e4642f557f
8 changed files with 109 additions and 100 deletions

View File

@ -72,11 +72,11 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
// Handshake // Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
this.getWebSocketLocation(req), null, false); this.getWebSocketLocation(req), null, false);
this.handshaker = wsFactory.newHandshaker(ctx, req); this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) { if (this.handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx); wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
} else { } else {
this.handshaker.performOpeningHandshake(ctx, req); this.handshaker.performOpeningHandshake(ctx.getChannel(), req);
} }
} }
@ -85,7 +85,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
.format("Channel %s received %s", ctx.getChannel().getId(), frame.getClass().getSimpleName())); .format("Channel %s received %s", ctx.getChannel().getId(), frame.getClass().getSimpleName()));
if (frame instanceof CloseWebSocketFrame) { if (frame instanceof CloseWebSocketFrame) {
this.handshaker.performClosingHandshake(ctx, (CloseWebSocketFrame) frame); this.handshaker.performClosingHandshake(ctx.getChannel(), (CloseWebSocketFrame) frame);
} else if (frame instanceof PingWebSocketFrame) { } else if (frame instanceof PingWebSocketFrame) {
ctx.getChannel().write( ctx.getChannel().write(
new PongWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData())); new PongWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));

View File

@ -92,11 +92,11 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
// Handshake // Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
this.getWebSocketLocation(req), null, false); this.getWebSocketLocation(req), null, false);
this.handshaker = wsFactory.newHandshaker(ctx, req); this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) { if (this.handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx); wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
} else { } else {
this.handshaker.performOpeningHandshake(ctx, req); this.handshaker.performOpeningHandshake(ctx.getChannel(), req);
} }
} }
@ -104,7 +104,7 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
// Check for closing frame // Check for closing frame
if (frame instanceof CloseWebSocketFrame) { if (frame instanceof CloseWebSocketFrame) {
this.handshaker.performClosingHandshake(ctx, (CloseWebSocketFrame) frame); this.handshaker.performClosingHandshake(ctx.getChannel(), (CloseWebSocketFrame) frame);
return; return;
} else if (frame instanceof PingWebSocketFrame) { } else if (frame instanceof PingWebSocketFrame) {
ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData())); ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData()));

View File

@ -92,11 +92,11 @@ public class WebSocketSslServerHandler extends SimpleChannelUpstreamHandler {
// Handshake // Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
this.getWebSocketLocation(req), null, false); this.getWebSocketLocation(req), null, false);
this.handshaker = wsFactory.newHandshaker(ctx, req); this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) { if (this.handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx); wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
} else { } else {
this.handshaker.performOpeningHandshake(ctx, req); this.handshaker.performOpeningHandshake(ctx.getChannel(), req);
} }
} }
@ -104,7 +104,7 @@ public class WebSocketSslServerHandler extends SimpleChannelUpstreamHandler {
// Check for closing frame // Check for closing frame
if (frame instanceof CloseWebSocketFrame) { if (frame instanceof CloseWebSocketFrame) {
this.handshaker.performClosingHandshake(ctx, (CloseWebSocketFrame) frame); this.handshaker.performClosingHandshake(ctx.getChannel(), (CloseWebSocketFrame) frame);
return; return;
} else if (frame instanceof PingWebSocketFrame) { } else if (frame instanceof PingWebSocketFrame) {
ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData())); ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData()));

View File

@ -20,7 +20,7 @@ import java.security.NoSuchAlgorithmException;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers; import io.netty.buffer.ChannelBuffers;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.Channel;
import io.netty.handler.codec.base64.Base64; import io.netty.handler.codec.base64.Base64;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
@ -40,7 +40,7 @@ public abstract class WebSocketServerHandshaker {
/** /**
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g
* "ws://myhost.com/mypath". Subsequent web socket frames will be * "ws://myhost.com/mypath". Subsequent web socket frames will be
@ -96,28 +96,28 @@ public abstract class WebSocketServerHandshaker {
/** /**
* Performs the opening handshake * Performs the opening handshake
* *
* @param ctx * @param channel
* Context * Channel
* @param req * @param req
* HTTP Request * HTTP Request
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
public abstract void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req); public abstract void performOpeningHandshake(Channel channel, HttpRequest req);
/** /**
* Performs the closing handshake * Performs the closing handshake
* *
* @param ctx * @param channel
* Context * Channel
* @param frame * @param frame
* Closing Frame that was received * Closing Frame that was received
*/ */
public abstract void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame); public abstract void performClosingHandshake(Channel channel, CloseWebSocketFrame frame);
/** /**
* Performs an MD5 hash * Performs an MD5 hash
* *
* @param bytes * @param bytes
* Data to hash * Data to hash
* @return Hashed data * @return Hashed data
@ -133,7 +133,7 @@ public abstract class WebSocketServerHandshaker {
/** /**
* SHA-1 hashing. Instance this we think it is not thread safe * SHA-1 hashing. Instance this we think it is not thread safe
* *
* @param bytes * @param bytes
* byte to hash * byte to hash
* @return hashed * @return hashed
@ -149,7 +149,7 @@ public abstract class WebSocketServerHandshaker {
/** /**
* Base 64 encoding * Base 64 encoding
* *
* @param bytes * @param bytes
* Bytes to encode * Bytes to encode
* @return encoded string * @return encoded string
@ -161,7 +161,7 @@ public abstract class WebSocketServerHandshaker {
/** /**
* Selects the first matching supported sub protocol * Selects the first matching supported sub protocol
* *
* @param requestedSubProtocol * @param requestedSubProtocol
* CSV of protocols to be supported. e.g. "chat, superchat" * CSV of protocols to be supported. e.g. "chat, superchat"
* @return First matching supported sub protocol. Null if not found. * @return First matching supported sub protocol. Null if not found.

View File

@ -32,11 +32,14 @@ import java.security.NoSuchAlgorithmException;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers; import io.netty.buffer.ChannelBuffers;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpChunkAggregator;
import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpHeaders.Values;
@ -60,7 +63,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
/** /**
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g
* "ws://myhost.com/mypath". Subsequent web socket frames will be * "ws://myhost.com/mypath". Subsequent web socket frames will be
@ -82,11 +85,11 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
* href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75" * href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75"
* >hixie-75</a>. * >hixie-75</a>.
* </p> * </p>
* *
* <p> * <p>
* Browser request to the server: * Browser request to the server:
* </p> * </p>
* *
* <pre> * <pre>
* GET /demo HTTP/1.1 * GET /demo HTTP/1.1
* Upgrade: WebSocket * Upgrade: WebSocket
@ -95,14 +98,14 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
* Origin: http://example.com * Origin: http://example.com
* Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5 * Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5
* Sec-WebSocket-Key2: 12998 5 Y3 1 .P00 * Sec-WebSocket-Key2: 12998 5 Y3 1 .P00
* *
* ^n:ds[4U * ^n:ds[4U
* </pre> * </pre>
* *
* <p> * <p>
* Server response: * Server response:
* </p> * </p>
* *
* <pre> * <pre>
* HTTP/1.1 101 WebSocket Protocol Handshake * HTTP/1.1 101 WebSocket Protocol Handshake
* Upgrade: WebSocket * Upgrade: WebSocket
@ -110,21 +113,21 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
* Sec-WebSocket-Origin: http://example.com * Sec-WebSocket-Origin: http://example.com
* Sec-WebSocket-Location: ws://example.com/demo * Sec-WebSocket-Location: ws://example.com/demo
* Sec-WebSocket-Protocol: sample * Sec-WebSocket-Protocol: sample
* *
* 8jKS'y:G*Co,Wxa- * 8jKS'y:G*Co,Wxa-
* </pre> * </pre>
* *
* @param ctx * @param channel
* Channel context * Channel
* @param req * @param req
* HTTP request * HTTP request
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
@Override @Override
public void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req) { public void performOpeningHandshake(Channel channel, HttpRequest req) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s web socket spec version 00 handshake", ctx.getChannel().getId())); logger.debug(String.format("Channel %s web socket spec version 00 handshake", channel.getId()));
} }
this.setVersion(WebSocketSpecificationVersion.V00); this.setVersion(WebSocketSpecificationVersion.V00);
@ -174,25 +177,25 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
} }
// Upgrade the connection and send the handshake response. // Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline(); ChannelPipeline p = channel.getPipeline();
p.remove("aggregator"); p.remove(HttpChunkAggregator.class);
p.replace("decoder", "wsdecoder", new WebSocket00FrameDecoder()); p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket00FrameDecoder());
ctx.getChannel().write(res); channel.write(res);
p.replace("encoder", "wsencoder", new WebSocket00FrameEncoder()); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket00FrameEncoder());
} }
/** /**
* Echo back the closing frame * Echo back the closing frame
* *
* @param ctx * @param channel
* Channel context * Channel
* @param frame * @param frame
* Web Socket frame that was received * Web Socket frame that was received
*/ */
@Override @Override
public void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame) { public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
ctx.getChannel().write(frame); channel.write(frame);
} }
} }

View File

@ -20,13 +20,16 @@ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpChunkAggregator;
import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.logging.InternalLogger; import io.netty.logging.InternalLogger;
@ -51,7 +54,7 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
/** /**
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g
* "ws://myhost.com/mypath". Subsequent web socket frames will be * "ws://myhost.com/mypath". Subsequent web socket frames will be
@ -73,11 +76,11 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-08">HyBi
* version 8 to 10</a>. Version 8, 9 and 10 share the same wire protocol. * version 8 to 10</a>. Version 8, 9 and 10 share the same wire protocol.
* </p> * </p>
* *
* <p> * <p>
* Browser request to the server: * Browser request to the server:
* </p> * </p>
* *
* <pre> * <pre>
* GET /chat HTTP/1.1 * GET /chat HTTP/1.1
* Host: server.example.com * Host: server.example.com
@ -88,11 +91,11 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
* Sec-WebSocket-Protocol: chat, superchat * Sec-WebSocket-Protocol: chat, superchat
* Sec-WebSocket-Version: 8 * Sec-WebSocket-Version: 8
* </pre> * </pre>
* *
* <p> * <p>
* Server response: * Server response:
* </p> * </p>
* *
* <pre> * <pre>
* HTTP/1.1 101 Switching Protocols * HTTP/1.1 101 Switching Protocols
* Upgrade: websocket * Upgrade: websocket
@ -100,18 +103,18 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat * Sec-WebSocket-Protocol: chat
* </pre> * </pre>
* *
* @param ctx * @param channel
* Channel context * Channel
* @param req * @param req
* HTTP request * HTTP request
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
@Override @Override
public void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req) { public void performOpeningHandshake(Channel channel, HttpRequest req) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s web socket spec version 10 handshake", ctx.getChannel().getId())); logger.debug(String.format("Channel %s web socket spec version 10 handshake", channel.getId()));
} }
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols")); HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols"));
@ -139,27 +142,27 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol)); res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol));
} }
ctx.getChannel().write(res); channel.write(res);
// Upgrade the connection and send the handshake response. // Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline(); ChannelPipeline p = channel.getPipeline();
p.remove("aggregator"); p.remove(HttpChunkAggregator.class);
p.replace("decoder", "wsdecoder", new WebSocket08FrameDecoder(true, this.allowExtensions)); p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket08FrameDecoder(true, this.allowExtensions));
p.replace("encoder", "wsencoder", new WebSocket08FrameEncoder(false)); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket08FrameEncoder(false));
} }
/** /**
* Echo back the closing frame and close the connection * Echo back the closing frame and close the connection
* *
* @param ctx * @param channel
* Channel context * Channel
* @param frame * @param frame
* Web Socket frame that was received * Web Socket frame that was received
*/ */
@Override @Override
public void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame) { public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
ChannelFuture f = ctx.getChannel().write(frame); ChannelFuture f = channel.write(frame);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }

View File

@ -20,13 +20,16 @@ import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpChunkAggregator;
import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.logging.InternalLogger; import io.netty.logging.InternalLogger;
@ -51,7 +54,7 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
/** /**
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g
* "ws://myhost.com/mypath". Subsequent web socket frames will be * "ws://myhost.com/mypath". Subsequent web socket frames will be
@ -73,11 +76,11 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
* "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">HyBi * "http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-17">HyBi
* versions 13-17</a>. Versions 13-17 share the same wire protocol. * versions 13-17</a>. Versions 13-17 share the same wire protocol.
* </p> * </p>
* *
* <p> * <p>
* Browser request to the server: * Browser request to the server:
* </p> * </p>
* *
* <pre> * <pre>
* GET /chat HTTP/1.1 * GET /chat HTTP/1.1
* Host: server.example.com * Host: server.example.com
@ -88,11 +91,11 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
* Sec-WebSocket-Protocol: chat, superchat * Sec-WebSocket-Protocol: chat, superchat
* Sec-WebSocket-Version: 13 * Sec-WebSocket-Version: 13
* </pre> * </pre>
* *
* <p> * <p>
* Server response: * Server response:
* </p> * </p>
* *
* <pre> * <pre>
* HTTP/1.1 101 Switching Protocols * HTTP/1.1 101 Switching Protocols
* Upgrade: websocket * Upgrade: websocket
@ -100,18 +103,18 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
* Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= * Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
* Sec-WebSocket-Protocol: chat * Sec-WebSocket-Protocol: chat
* </pre> * </pre>
* *
* @param ctx * @param channel
* Channel context * Channel
* @param req * @param req
* HTTP request * HTTP request
* @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException
*/ */
@Override @Override
public void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req) { public void performOpeningHandshake(Channel channel, HttpRequest req) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Channel %s web socket spec version 17 handshake", ctx.getChannel().getId())); logger.debug(String.format("Channel %s web socket spec version 17 handshake", channel.getId()));
} }
HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols")); HttpResponse res = new DefaultHttpResponse(HTTP_1_1, new HttpResponseStatus(101, "Switching Protocols"));
@ -139,27 +142,27 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol)); res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol));
} }
ctx.getChannel().write(res); channel.write(res);
// Upgrade the connection and send the handshake response. // Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline(); ChannelPipeline p = channel.getPipeline();
p.remove("aggregator"); p.remove(HttpChunkAggregator.class);
p.replace("decoder", "wsdecoder", new WebSocket13FrameDecoder(true, this.allowExtensions)); p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket13FrameDecoder(true, this.allowExtensions));
p.replace("encoder", "wsencoder", new WebSocket13FrameEncoder(false)); p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false));
} }
/** /**
* Echo back the closing frame and close the connection * Echo back the closing frame and close the connection
* *
* @param ctx * @param channel
* Channel context * Channel
* @param frame * @param frame
* Web Socket frame that was received * Web Socket frame that was received
*/ */
@Override @Override
public void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame) { public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
ChannelFuture f = ctx.getChannel().write(frame); ChannelFuture f = channel.write(frame);
f.addListener(ChannelFutureListener.CLOSE); f.addListener(ChannelFutureListener.CLOSE);
} }

View File

@ -15,7 +15,7 @@
*/ */
package io.netty.handler.codec.http.websocketx; package io.netty.handler.codec.http.websocketx;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.Channel;
import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
@ -36,7 +36,7 @@ public class WebSocketServerHandshakerFactory {
/** /**
* Constructor specifying the destination web socket location * Constructor specifying the destination web socket location
* *
* @param webSocketURL * @param webSocketURL
* URL for web socket communications. e.g * URL for web socket communications. e.g
* "ws://myhost.com/mypath". Subsequent web socket frames will be * "ws://myhost.com/mypath". Subsequent web socket frames will be
@ -56,11 +56,11 @@ public class WebSocketServerHandshakerFactory {
/** /**
* Instances a new handshaker * Instances a new handshaker
* *
* @return A new WebSocketServerHandshaker for the requested web socket * @return A new WebSocketServerHandshaker for the requested web socket
* version. Null if web socket version is not supported. * version. Null if web socket version is not supported.
*/ */
public WebSocketServerHandshaker newHandshaker(ChannelHandlerContext ctx, HttpRequest req) { public WebSocketServerHandshaker newHandshaker(HttpRequest req) {
String version = req.getHeader(Names.SEC_WEBSOCKET_VERSION); String version = req.getHeader(Names.SEC_WEBSOCKET_VERSION);
if (version != null) { if (version != null) {
@ -83,16 +83,16 @@ public class WebSocketServerHandshakerFactory {
/** /**
* Return that we need cannot not support the web socket version * Return that we need cannot not support the web socket version
* *
* @param ctx * @param channel
* Context * Channel
*/ */
public void sendUnsupportedWebSocketVersionResponse(ChannelHandlerContext ctx) { public void sendUnsupportedWebSocketVersionResponse(Channel channel) {
HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(101, HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(101,
"Switching Protocols")); "Switching Protocols"));
res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED); res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED);
res.setHeader(Names.SEC_WEBSOCKET_VERSION, "13"); res.setHeader(Names.SEC_WEBSOCKET_VERSION, "13");
ctx.getChannel().write(res); channel.write(res);
} }
} }