Skip to content

Commit

Permalink
Use websocket wire protocol version rather than specification version
Browse files Browse the repository at this point in the history
  • Loading branch information
veebs committed Dec 15, 2011
1 parent f01d8a4 commit 812a79f
Show file tree
Hide file tree
Showing 52 changed files with 2,972 additions and 2,951 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
import io.netty.channel.socket.nio.NioServerSocketChannelFactory;

/**
* A Web Socket echo server for running the <a href="http://www.tavendo.de/autobahn/testsuite.html">autobahn</a>
* test suite
* A Web Socket echo server for running the <a href="http://www.tavendo.de/autobahn/testsuite.html">autobahn</a> test
* suite
*/
public class WebSocketServer {
public static void main(String[] args) {
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.FINE);
Logger.getLogger("").addHandler(ch);
Logger.getLogger("").setLevel(Level.FINE);

// Configure the server.
ServerBootstrap bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
//bootstrap.setOption("child.tcpNoDelay", true);

// bootstrap.setOption("child.tcpNoDelay", true);

// Set up the event pipeline factory.
bootstrap.setPipelineFactory(new WebSocketServerPipelineFactory());

// Bind and start to accept incoming connections.
bootstrap.bind(new InetSocketAddress(9000));

System.out.println("Web Socket Server started on localhost:9000.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame fram
ctx.getChannel().write(
new PongWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
} else if (frame instanceof TextWebSocketFrame) {
//String text = ((TextWebSocketFrame) frame).getText();
ctx.getChannel().write(new TextWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
// String text = ((TextWebSocketFrame) frame).getText();
ctx.getChannel().write(
new TextWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
} else if (frame instanceof BinaryWebSocketFrame) {
ctx.getChannel().write(
new BinaryWebSocketFrame(frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* <p>08. Go to test suite directory: <tt>cd testsuite/websockets</tt>
*
* <p>09. Edit <tt>fuzzing_clinet_spec.json</tt> and set the version to 10 or 17.
* <p>09. Edit <tt>fuzzing_clinet_spec.json</tt> and set the hybi specification version to 10 or 17 (RFC 6455).
* <code>
* {
* "options": {"failByDrop": false},
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/io/netty/example/http/websocketx/client/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketSpecificationVersion;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;

/**
* A HTTP client demo app
Expand All @@ -55,31 +55,31 @@ public static void runClient() throws Exception {
MyCallbackHandler callbackHandler = new MyCallbackHandler();
WebSocketClientFactory factory = new WebSocketClientFactory();

// Connect with spec version 17. You can change it to V10 or V00.
// Connect with RFC 6455. 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);
WebSocketClient client = factory.newClient(new URI("ws://localhost:8080/websocket"), WebSocketVersion.V13,
callbackHandler, null);

// Connect
System.out.println("WebSocket Client connecting");
client.connect().awaitUninterruptibly();
System.out.println("WebSocket Client connecting");
client.connect().awaitUninterruptibly();
Thread.sleep(200);

// Send 10 messages and wait for responses
System.out.println("WebSocket Client sending message");
System.out.println("WebSocket Client sending message");
for (int i = 0; i < 10; i++) {
client.send(new TextWebSocketFrame("Message #" + i));
}
Thread.sleep(1000);

// Ping - only supported for V10 and up.
System.out.println("WebSocket Client sending ping");
System.out.println("WebSocket Client sending ping");
client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
Thread.sleep(1000);

// Close
System.out.println("WebSocket Client sending close");
System.out.println("WebSocket Client sending close");
client.send(new CloseWebSocketFrame());
Thread.sleep(1000);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,44 @@

import io.netty.handler.codec.http.websocketx.WebSocketFrame;


/**
* Copied from https://github.com/cgbystrom/netty-tools
*
* Callbacks for the {@link WebSocketClient}. Implement and get notified when events happen.
*/
public interface WebSocketCallback {

/**
* Called when the client is connected to the server
*
* @param client
* Current client used to connect
*/
void onConnect(WebSocketClient client);
/**
* Called when the client is connected to the server
*
* @param client
* Current client used to connect
*/
void onConnect(WebSocketClient client);

/**
* Called when the client got disconnected from the server.
*
* @param client
* Current client that was disconnected
*/
void onDisconnect(WebSocketClient client);
/**
* Called when the client got disconnected from the server.
*
* @param client
* Current client that was disconnected
*/
void onDisconnect(WebSocketClient client);

/**
* Called when a message arrives from the server.
*
* @param client
* Current client connected
* @param frame
* Data received from server
*/
void onMessage(WebSocketClient client, WebSocketFrame frame);
/**
* Called when a message arrives from the server.
*
* @param client
* Current client connected
* @param frame
* Data received from server
*/
void onMessage(WebSocketClient client, WebSocketFrame frame);

/**
* Called when an unhandled errors occurs.
*
* @param t
* The causing error
*/
void onError(Throwable t);
/**
* Called when an unhandled errors occurs.
*
* @param t
* The causing error
*/
void onError(Throwable t);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,36 +29,26 @@
*/
public interface WebSocketClient {

/**
* Connect to server Host and port is setup by the factory.
*
* @return Connect future. Fires when connected.
*/
ChannelFuture connect();
/**
* Connect to server Host and port is setup by the factory.
*
* @return Connect future. Fires when connected.
*/
ChannelFuture connect();

/**
* Disconnect from the server
*
* @return Disconnect future. Fires when disconnected.
*/
ChannelFuture disconnect();
/**
* Disconnect from the server
*
* @return Disconnect future. Fires when disconnected.
*/
ChannelFuture disconnect();

/**
* Send data to server
*
* @param frame
* Data for sending
* @return Write future. Will fire when the data is sent.
*/
ChannelFuture send(WebSocketFrame frame);

/**
* Adds a custom header to this client request
*
* @param header
* Name of header field to add to request
* @param value
* Value of header field added to request
*/
void addCustomHeader(String header, String value);
/**
* Send data to server
*
* @param frame
* Data for sending
* @return Write future. Will fire when the data is sent.
*/
ChannelFuture send(WebSocketFrame frame);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
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 io.netty.handler.codec.http.websocketx.WebSocketVersion;

import java.net.URI;
import java.util.Map;
import java.util.concurrent.Executors;

/**
Expand All @@ -42,48 +43,50 @@
*/
public class WebSocketClientFactory {

private final NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
private final NioClientSocketChannelFactory socketChannelFactory = new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(), Executors.newCachedThreadPool());

/**
* Create a new WebSocket client.
*
* @param url
* URL to connect to.
* @param version
* Web Socket version to support
* @param callback
* Callback interface to receive events
* @return A WebSocket client. Call {@link WebSocketClient#connect()} to connect.
*/
public WebSocketClient newClient(final URI url,
final WebSocketSpecificationVersion version,
final WebSocketCallback callback) {
ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);
/**
* Create a new WebSocket client.
*
* @param url
* URL to connect to.
* @param version
* Web Socket version to support
* @param callback
* Callback interface to receive events
* @param customHeaders
* custom HTTP headers to pass during the handshake
* @return A WebSocket client. Call {@link WebSocketClient#connect()} to connect.
*/
public WebSocketClient newClient(final URI url, final WebSocketVersion version, final WebSocketCallback callback,
Map<String, String> customHeaders) {
ClientBootstrap bootstrap = new ClientBootstrap(socketChannelFactory);

String protocol = url.getScheme();
if (!protocol.equals("ws") && !protocol.equals("wss")) {
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
}
String protocol = url.getScheme();
if (!protocol.equals("ws") && !protocol.equals("wss")) {
throw new IllegalArgumentException("Unsupported protocol: " + protocol);
}

final WebSocketClientHandler clientHandler = new WebSocketClientHandler(bootstrap, url, version, callback);
final WebSocketClientHandler clientHandler = new WebSocketClientHandler(bootstrap, url, version, callback,
customHeaders);

bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();

// 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;
}
});
@Override
public ChannelPipeline getPipeline() throws Exception {
ChannelPipeline pipeline = Channels.pipeline();

return clientHandler;
}
// 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;
}
});

return clientHandler;
}
}
Loading

0 comments on commit 812a79f

Please sign in to comment.