Back port #115 and #117 to 3.2 branch

This commit is contained in:
vibul 2011-12-11 22:50:19 +11:00
parent 24ef8a5259
commit a664e92244
16 changed files with 161 additions and 155 deletions

View File

@ -47,6 +47,6 @@ public class WebSocketServer {
// Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(9000));
System.out.println("Web Socket Server started on 9000. Open your browser and navigate to http://localhost:9000/");
System.out.println("Web Socket Server started on localhost:9000.");
}
}

View File

@ -72,11 +72,11 @@ public class WebSocketServerHandler extends SimpleChannelUpstreamHandler {
// Handshake
WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
this.getWebSocketLocation(req), null, false);
this.handshaker = wsFactory.newHandshaker(ctx, req);
this.handshaker = wsFactory.newHandshaker(req);
if (this.handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx);
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel());
} 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()));
if (frame instanceof CloseWebSocketFrame) {
this.handshaker.performClosingHandshake(ctx, (CloseWebSocketFrame) frame);
this.handshaker.performClosingHandshake(ctx.getChannel(), (CloseWebSocketFrame) frame);
} else if (frame instanceof PingWebSocketFrame) {
ctx.getChannel().write(
new PongWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));

View File

@ -55,8 +55,9 @@ public class App {
MyCallbackHandler callbackHandler = new MyCallbackHandler();
WebSocketClientFactory factory = new WebSocketClientFactory();
// Connect with spec version 17 (try changing it to V10 or V00 and it will
// still work ... fingers crossed ;-)
// Connect with spec version 17. You can change it to V10 or V00.
// If you change it to V00, ping is not supported and remember to change HttpResponseDecoder to
// WebSocketHttpResponseDecoder in the pipeline.
WebSocketClient client = factory.newClient(new URI("ws://localhost:8080/websocket"),
WebSocketSpecificationVersion.V17, callbackHandler);

View File

@ -28,6 +28,7 @@ import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketSpecificationVersion;
import java.net.URI;
@ -71,7 +72,11 @@ public class WebSocketClientFactory {
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();
pipeline.addLast("decoder", new WebSocketHttpResponseDecoder());
// If you wish to support HyBi V00, you need to use WebSocketHttpResponseDecoder instead for
// HttpResponseDecoder.
pipeline.addLast("decoder", new HttpResponseDecoder());
pipeline.addLast("encoder", new HttpRequestEncoder());
pipeline.addLast("ws-handler", clientHandler);
return pipeline;

View File

@ -66,7 +66,7 @@ public class WebSocketClientHandler extends SimpleChannelUpstreamHandler impleme
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
channel = e.getChannel();
this.handshaker = new WebSocketClientHandshakerFactory().newHandshaker(url, version, null, false);
handshaker.performOpeningHandshake(ctx, channel);
handshaker.performOpeningHandshake(channel);
}
@Override
@ -77,7 +77,7 @@ public class WebSocketClientHandler extends SimpleChannelUpstreamHandler impleme
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (!handshaker.isOpeningHandshakeCompleted()) {
handshaker.performClosingHandshake(ctx, (HttpResponse) e.getMessage());
handshaker.performClosingHandshake(ctx.getChannel(), (HttpResponse) e.getMessage());
callback.onConnect(this);
return;
}

View File

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

View File

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

View File

@ -22,7 +22,6 @@ import java.security.NoSuchAlgorithmException;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.base64.Base64;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.util.CharsetUtil;
@ -114,22 +113,20 @@ public abstract class WebSocketClientHandshaker {
/**
* Performs the opening handshake
*
* @param ctx
* Channel context
* @param channel
* Channel
*/
public abstract void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel);
public abstract void performOpeningHandshake(Channel channel);
/**
* Performs the closing handshake
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param response
* HTTP response containing the closing handshake details
*/
public abstract void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException;
public abstract void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException;
/**
* Performs an MD5 hash

View File

@ -21,13 +21,14 @@ import java.util.Arrays;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import org.jboss.netty.handler.codec.http.HttpHeaders.Values;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
@ -81,13 +82,11 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
* ^n:ds[4U
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel into which we can write our request
*/
@Override
public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) {
public void performOpeningHandshake(Channel channel) {
// Make keys
int spaces1 = createRandomNumber(1, 12);
int spaces2 = createRandomNumber(1, 12);
@ -147,7 +146,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
channel.write(request);
ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket00FrameEncoder());
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder());
}
/**
@ -166,15 +165,15 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
* 8jKS'y:G*Co,Wxa-
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param response
* HTTP response returned from the server for the request sent by
* beginOpeningHandshake00().
* @throws WebSocketHandshakeException
*/
@Override
public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException {
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake");
if (!response.getStatus().equals(status)) {
@ -199,7 +198,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker {
String protocol = response.getHeader(Names.SEC_WEBSOCKET_PROTOCOL);
this.setSubProtocolResponse(protocol);
ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocket00FrameDecoder());
channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket00FrameDecoder());
this.setOpenningHandshakeCompleted(true);
}

View File

@ -18,13 +18,14 @@ package org.jboss.netty.handler.codec.http.websocketx;
import java.net.URI;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import org.jboss.netty.handler.codec.http.HttpHeaders.Values;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.logging.InternalLogger;
@ -90,13 +91,11 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker {
* Sec-WebSocket-Version: 8
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel into which we can write our request
*/
@Override
public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) {
public void performOpeningHandshake(Channel channel) {
// Get path
URI wsURL = this.getWebSocketURL();
String path = wsURL.getPath();
@ -130,7 +129,7 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker {
channel.write(request);
ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket08FrameEncoder(true));
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket08FrameEncoder(true));
}
/**
@ -146,15 +145,15 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker {
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param response
* HTTP response returned from the server for the request sent by
* beginOpeningHandshake00().
* @throws WebSocketHandshakeException
*/
@Override
public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException {
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
final HttpResponseStatus status = new HttpResponseStatus(101, "Switching Protocols");
if (!response.getStatus().equals(status)) {
@ -176,7 +175,7 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker {
throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, this.expectedChallengeResponseString));
}
ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocket08FrameDecoder(false, this.allowExtensions));
channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket08FrameDecoder(false, this.allowExtensions));
this.setOpenningHandshakeCompleted(true);
}

View File

@ -18,13 +18,14 @@ package org.jboss.netty.handler.codec.http.websocketx;
import java.net.URI;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import org.jboss.netty.handler.codec.http.HttpHeaders.Values;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestEncoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseDecoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpVersion;
import org.jboss.netty.logging.InternalLogger;
@ -90,13 +91,11 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker {
* Sec-WebSocket-Version: 13
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel into which we can write our request
*/
@Override
public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) {
public void performOpeningHandshake(Channel channel) {
// Get path
URI wsURL = this.getWebSocketURL();
String path = wsURL.getPath();
@ -130,7 +129,7 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker {
channel.write(request);
ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket13FrameEncoder(true));
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket13FrameEncoder(true));
}
/**
@ -146,15 +145,15 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker {
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param response
* HTTP response returned from the server for the request sent by
* beginOpeningHandshake00().
* @throws WebSocketHandshakeException
*/
@Override
public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException {
public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException {
final HttpResponseStatus status = new HttpResponseStatus(101, "Switching Protocols");
if (!response.getStatus().equals(status)) {
@ -176,7 +175,7 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker {
throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, this.expectedChallengeResponseString));
}
ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocket13FrameDecoder(false, this.allowExtensions));
channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket13FrameDecoder(false, this.allowExtensions));
this.setOpenningHandshakeCompleted(true);
}

View File

@ -20,14 +20,11 @@ import java.security.NoSuchAlgorithmException;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.base64.Base64;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.util.CharsetUtil;
/**
* Base class for server side web socket opening and closing handshakes
*/
public abstract class WebSocketServerHandshaker {
private String webSocketURL;
@ -97,23 +94,23 @@ public abstract class WebSocketServerHandshaker {
/**
* Performs the opening handshake
*
* @param ctx
* Context
* @param channel
* Channel
* @param req
* HTTP Request
* @throws NoSuchAlgorithmException
*/
public abstract void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req);
public abstract void performOpeningHandshake(Channel channel, HttpRequest req);
/**
* Performs the closing handshake
*
* @param ctx
* Context
* @param channel
* Channel
* @param frame
* 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

View File

@ -32,11 +32,14 @@ import java.security.NoSuchAlgorithmException;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import org.jboss.netty.handler.codec.http.HttpHeaders.Values;
@ -114,17 +117,17 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
* 8jKS'y:G*Co,Wxa-
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param req
* HTTP request
* @throws NoSuchAlgorithmException
*/
@Override
public void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req) {
public void performOpeningHandshake(Channel channel, HttpRequest req) {
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);
@ -174,25 +177,25 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
}
// Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocket00FrameDecoder());
ChannelPipeline p = channel.getPipeline();
p.remove(HttpChunkAggregator.class);
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
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param frame
* Web Socket frame that was received
*/
@Override
public void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame) {
ctx.getChannel().write(frame);
public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
channel.write(frame);
}
}

View File

@ -20,13 +20,16 @@ import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.security.NoSuchAlgorithmException;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import org.jboss.netty.logging.InternalLogger;
@ -101,17 +104,17 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param req
* HTTP request
* @throws NoSuchAlgorithmException
*/
@Override
public void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req) {
public void performOpeningHandshake(Channel channel, HttpRequest req) {
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"));
@ -139,27 +142,27 @@ public class WebSocketServerHandshaker10 extends WebSocketServerHandshaker {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol));
}
ctx.getChannel().write(res);
channel.write(res);
// Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocket08FrameDecoder(true, this.allowExtensions));
p.replace("encoder", "wsencoder", new WebSocket08FrameEncoder(false));
ChannelPipeline p = channel.getPipeline();
p.remove(HttpChunkAggregator.class);
p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket08FrameDecoder(true, this.allowExtensions));
p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket08FrameEncoder(false));
}
/**
* Echo back the closing frame and close the connection
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param frame
* Web Socket frame that was received
*/
@Override
public void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame) {
ChannelFuture f = ctx.getChannel().write(frame);
public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
ChannelFuture f = channel.write(frame);
f.addListener(ChannelFutureListener.CLOSE);
}

View File

@ -20,13 +20,16 @@ import static org.jboss.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import java.security.NoSuchAlgorithmException;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
import org.jboss.netty.handler.codec.http.HttpResponseStatus;
import org.jboss.netty.handler.codec.http.HttpHeaders.Names;
import org.jboss.netty.logging.InternalLogger;
@ -101,17 +104,17 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
* Sec-WebSocket-Protocol: chat
* </pre>
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param req
* HTTP request
* @throws NoSuchAlgorithmException
*/
@Override
public void performOpeningHandshake(ChannelHandlerContext ctx, HttpRequest req) {
public void performOpeningHandshake(Channel channel, HttpRequest req) {
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"));
@ -139,27 +142,27 @@ public class WebSocketServerHandshaker17 extends WebSocketServerHandshaker {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, this.selectSubProtocol(protocol));
}
ctx.getChannel().write(res);
channel.write(res);
// Upgrade the connection and send the handshake response.
ChannelPipeline p = ctx.getChannel().getPipeline();
p.remove("aggregator");
p.replace("decoder", "wsdecoder", new WebSocket13FrameDecoder(true, this.allowExtensions));
p.replace("encoder", "wsencoder", new WebSocket13FrameEncoder(false));
ChannelPipeline p = channel.getPipeline();
p.remove(HttpChunkAggregator.class);
p.replace(HttpRequestDecoder.class, "wsdecoder", new WebSocket13FrameDecoder(true, this.allowExtensions));
p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false));
}
/**
* Echo back the closing frame and close the connection
*
* @param ctx
* Channel context
* @param channel
* Channel
* @param frame
* Web Socket frame that was received
*/
@Override
public void performClosingHandshake(ChannelHandlerContext ctx, CloseWebSocketFrame frame) {
ChannelFuture f = ctx.getChannel().write(frame);
public void performClosingHandshake(Channel channel, CloseWebSocketFrame frame) {
ChannelFuture f = channel.write(frame);
f.addListener(ChannelFutureListener.CLOSE);
}

View File

@ -15,7 +15,7 @@
*/
package org.jboss.netty.handler.codec.http.websocketx;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.DefaultHttpResponse;
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpResponse;
@ -60,7 +60,7 @@ public class WebSocketServerHandshakerFactory {
* @return A new WebSocketServerHandshaker for the requested web socket
* 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);
if (version != null) {
@ -84,15 +84,15 @@ public class WebSocketServerHandshakerFactory {
/**
* Return that we need cannot not support the web socket version
*
* @param ctx
* Context
* @param channel
* Channel
*/
public void sendUnsupportedWebSocketVersionResponse(ChannelHandlerContext ctx) {
public void sendUnsupportedWebSocketVersionResponse(Channel channel) {
HttpResponse res = new DefaultHttpResponse(HttpVersion.HTTP_1_1, new HttpResponseStatus(101,
"Switching Protocols"));
res.setStatus(HttpResponseStatus.UPGRADE_REQUIRED);
res.setHeader(Names.SEC_WEBSOCKET_VERSION, "13");
ctx.getChannel().write(res);
channel.write(res);
}
}