Skip to content

Commit

Permalink
Improve encoder/decoder "replace" in Web Socket Client handshake.
Browse files Browse the repository at this point in the history
  • Loading branch information
veebs committed Dec 11, 2011
1 parent dc16558 commit 5be24a0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ public static void main(String[] args) {
// 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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ public static void runClient() throws Exception {
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);

Expand All @@ -72,7 +73,7 @@ public static void runClient() throws Exception {
}
Thread.sleep(1000);

// Ping
// Ping - only supported for V10 and up.
System.out.println("WebSocket Client sending ping");
client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
Thread.sleep(1000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.netty.channel.Channels;
import io.netty.channel.socket.nio.NioClientSocketChannelFactory;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.websocketx.WebSocketSpecificationVersion;

import java.net.URI;
Expand All @@ -45,7 +46,7 @@ public class WebSocketClientFactory {
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

/**
* Create a new WebSocket client
* Create a new WebSocket client.
*
* @param url
* URL to connect to.
Expand All @@ -72,7 +73,11 @@ public WebSocketClient newClient(final URI url,
@Override
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public WebSocketClientHandler(ClientBootstrap bootstrap, URI url, WebSocketSpeci
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
Expand All @@ -77,7 +77,7 @@ public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws
@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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.base64.Base64;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil;
Expand Down Expand Up @@ -114,22 +113,20 @@ protected void setSubProtocolResponse(String subProtocolResponse) {
/**
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

import io.netty.buffer.ChannelBuffers;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

Expand Down Expand Up @@ -81,13 +82,11 @@ public WebSocketClientHandshaker00(URI webSocketURL, WebSocketSpecificationVersi
* ^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);
Expand Down Expand Up @@ -147,7 +146,7 @@ public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel)

channel.write(request);

ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket00FrameEncoder());
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder());
}

/**
Expand All @@ -166,15 +165,15 @@ public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel)
* 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)) {
Expand All @@ -199,7 +198,7 @@ public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse resp
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import java.net.URI;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.logging.InternalLogger;
Expand Down Expand Up @@ -90,13 +91,11 @@ public WebSocketClientHandshaker10(URI webSocketURL, WebSocketSpecificationVersi
* 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();
Expand Down Expand Up @@ -130,7 +129,7 @@ public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel)

channel.write(request);

ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket08FrameEncoder(true));
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket08FrameEncoder(true));
}

/**
Expand All @@ -146,15 +145,15 @@ public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel)
* 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)) {
Expand All @@ -176,7 +175,7 @@ public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse resp
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import java.net.URI;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders.Names;
import io.netty.handler.codec.http.HttpHeaders.Values;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.logging.InternalLogger;
Expand Down Expand Up @@ -90,13 +91,11 @@ public WebSocketClientHandshaker17(URI webSocketURL, WebSocketSpecificationVersi
* 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();
Expand Down Expand Up @@ -130,7 +129,7 @@ public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel)

channel.write(request);

ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket13FrameEncoder(true));
channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket13FrameEncoder(true));
}

/**
Expand All @@ -146,15 +145,15 @@ public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel)
* 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)) {
Expand All @@ -176,7 +175,7 @@ public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse resp
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);
}
Expand Down

0 comments on commit 5be24a0

Please sign in to comment.