package org.chililog.client.websocket; import org.chililog.server.pubsub.websocket.WebSocketSpecificationVersion; import org.jboss.netty.bootstrap.ClientBootstrap; import org.jboss.netty.channel.ChannelPipeline; 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 java.net.URI; import java.util.concurrent.Executors; /** * Copied from https://github.com/cgbystrom/netty-tools * * A factory for creating WebSocket clients. The entry point for creating and connecting a client. Can and should be * used to create multiple instances. * * @author <a href="http://www.pedantique.org/">Carl Byström</a> */ public class WebSocketClientFactory { private 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); 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); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); pipeline.addLast("decoder", new WebSocketHttpResponseDecoder()); pipeline.addLast("encoder", new HttpRequestEncoder()); pipeline.addLast("ws-handler", clientHandler); return pipeline; } }); return clientHandler; } }