List of usage examples for io.netty.handler.codec.http.websocketx WebSocketServerProtocolHandler WebSocketServerProtocolHandler
public WebSocketServerProtocolHandler(String websocketPath)
From source file:be.yildizgames.module.network.netty.NettyChannelInitializer.java
License:MIT License
@Override protected void initChannel(final SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); switch (factory.getCodec()) { case STRING://from w w w. jav a2 s . c o m pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); break; case HTTP: pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new ChunkedWriteHandler()); break; case WEBSOCKET: if (this.factory.isServer()) { pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new WebSocketServerProtocolHandler("/websocket")); } else { pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(8192)); } break; default: throw new IllegalArgumentException("Unknown codec: " + factory.getCodec()); } pipeline.addLast("handler", this.factory.create()); }
From source file:co.rsk.rpc.netty.Web3WebSocketServer.java
License:Open Source License
public void start() throws InterruptedException { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from ww w .j a v a2 s . c om*/ protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(1024 * 1024 * 5)); p.addLast(new WebSocketServerProtocolHandler("/websocket")); p.addLast(jsonRpcHandler); p.addLast(web3ServerHandler); p.addLast(new Web3ResultWebSocketResponseHandler()); } }); webSocketChannel = b.bind(host, port); webSocketChannel.sync(); }
From source file:com.barchart.netty.server.http.pipeline.HttpRequestChannelHandler.java
License:BSD License
private void handleWebSocket(final ChannelHandlerContext ctx, final FullHttpRequest msg) throws Exception { // Check if this path is a websocket final HandlerFactory<? extends ChannelHandler> factory = server.webSocketFactory(msg.getUri()); if (factory == null) { sendServerError(ctx, new ServerException(HttpResponseStatus.NOT_IMPLEMENTED, "Websocket upgrade not available at this path")); } else {/*from www.java2 s .c o m*/ // TODO log request // server.logger().access(request, 0); ctx.pipeline().addLast( // Handshaker new WebSocketServerProtocolHandler(msg.getUri()), // Fires channelActive() after handshake and removes self new WebSocketConnectedNotifier(), // BinaryWebSocketFrame <-> ByteBuf codec before user codecs new WebSocketBinaryCodec(), // Handlers should add any other codecs they need to the // pipeline using their handlerAdded() method. factory.newHandler()); // Relay handshake to websocket handler ctx.fireChannelRead(msg); // Remove self from pipeline ctx.pipeline().remove(this); } }
From source file:com.mpcc.springmvc.configuration.ServerInitializer.java
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); pipeline.addLast(new WebSocketServerProtocolHandler("/websocket")); pipeline.addLast(new ServerHandler()); }
From source file:de.ruedigermoeller.reallive.client.wsgate.WebSocketGate.java
License:Open Source License
public void run() { // param(1) just example to show the thread model with 2 connected clients while live coding // EventLoopGroup bossGroup = new NioEventLoopGroup(1); // param(1) just example to show the thread model with 2 connected clients while live coding EventLoopGroup workerGroup = new NioEventLoopGroup(8); try {/*from w w w .j a v a 2s . c om*/ // additional thread pool for blocking handler // final EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(8); final ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpRequestDecoder(), new HttpObjectAggregator(65536), new HttpResponseEncoder(), new WebSocketServerProtocolHandler("/websocket"), new WebSocketGateChannelHandler(WebSocketGate.this)); // normal example without another thread pool // register blocking or long lasting handler to additional thread pool // ch.pipeline().addLast(executorGroup, new JSUGWebSocketHandler(channels)); } }); final Channel channel; channel = bootstrap.bind(port).sync().channel(); System.out.println("server started on port: " + port); channel.closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdown(); } }
From source file:deathcap.wsmc.web.ServerHandler.java
License:Apache License
@Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("codec-http", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); pipeline.addLast("handler", new HTTPHandler(this.webThread.wsPort)); pipeline.addLast("websocket", new WebSocketServerProtocolHandler("/server")); pipeline.addLast("websocket-handler", new WebSocketHandler(webThread, this.mcAddress, this.mcPort, this.users, this.filter, this.verbose)); }
From source file:io.reactivesocket.netty.websocket.ClientServerTest.java
License:Apache License
@BeforeClass public static void setup() throws Exception { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { @Override/*from w w w. j ava2 s . co m*/ protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(64 * 1024)); pipeline.addLast(new WebSocketServerProtocolHandler("/rs")); pipeline.addLast(serverHandler); } }); serverChannel = b.bind("localhost", 8025).sync().channel(); ClientWebSocketDuplexConnection duplexConnection = RxReactiveStreams .toObservable(ClientWebSocketDuplexConnection.create( InetSocketAddress.createUnresolved("localhost", 8025), "/rs", new NioEventLoopGroup())) .toBlocking().single(); client = DefaultReactiveSocket.fromClientConnection(duplexConnection, ConnectionSetupPayload.create("UTF-8", "UTF-8"), t -> t.printStackTrace()); client.startAndWait(); }
From source file:io.reactivesocket.netty.websocket.Pong.java
License:Apache License
public static void main(String... args) throws Exception { byte[] response = new byte[1024]; Random r = new Random(); r.nextBytes(response);/* w w w .j a v a2s .c o m*/ ReactiveSocketServerHandler serverHandler = ReactiveSocketServerHandler .create((setupPayload, rs) -> new RequestHandler() { @Override public Publisher<Payload> handleRequestResponse(Payload payload) { return new Publisher<Payload>() { @Override public void subscribe(Subscriber<? super Payload> s) { Payload responsePayload = new Payload() { ByteBuffer data = ByteBuffer.wrap(response); ByteBuffer metadata = ByteBuffer.allocate(0); public ByteBuffer getData() { return data; } @Override public ByteBuffer getMetadata() { return metadata; } }; s.onNext(responsePayload); s.onComplete(); } }; } @Override public Publisher<Payload> handleRequestStream(Payload payload) { Payload response = TestUtil.utf8EncodedPayload("hello world", "metadata"); return RxReactiveStreams.toPublisher(Observable.range(1, 10).map(i -> response)); } @Override public Publisher<Payload> handleSubscription(Payload payload) { Payload response = TestUtil.utf8EncodedPayload("hello world", "metadata"); return RxReactiveStreams.toPublisher(Observable.range(1, 10).map(i -> response)); } @Override public Publisher<Void> handleFireAndForget(Payload payload) { return Subscriber::onComplete; } @Override public Publisher<Payload> handleChannel(Payload initialPayload, Publisher<Payload> inputs) { Observable<Payload> observable = RxReactiveStreams.toObservable(inputs).map(input -> input); return RxReactiveStreams.toPublisher(observable); // return outputSubscriber -> { // inputs.subscribe(new Subscriber<Payload>() { // private int count = 0; // private boolean completed = false; // // @Override // public void onSubscribe(Subscription s) { // //outputSubscriber.onSubscribe(s); // s.request(128); // } // // @Override // public void onNext(Payload input) { // if (completed) { // return; // } // count += 1; // outputSubscriber.onNext(input); // outputSubscriber.onNext(input); // if (count > 10) { // completed = true; // outputSubscriber.onComplete(); // } // } // // @Override // public void onError(Throwable t) { // if (!completed) { // outputSubscriber.onError(t); // } // } // // @Override // public void onComplete() { // if (!completed) { // outputSubscriber.onComplete(); // } // } // }); // }; } @Override public Publisher<Void> handleMetadataPush(Payload payload) { return null; } }); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(64 * 1024)); pipeline.addLast(new WebSocketServerProtocolHandler("/rs")); pipeline.addLast(serverHandler); } }); Channel localhost = b.bind("localhost", 8025).sync().channel(); localhost.closeFuture().sync(); }
From source file:io.reactivesocket.transport.websocket.ClientServerTest.java
License:Apache License
@BeforeClass public static void setup() throws Exception { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { @Override//w ww. ja va 2s. co m protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(64 * 1024)); pipeline.addLast(new WebSocketServerProtocolHandler("/rs")); pipeline.addLast(serverHandler); } }); serverChannel = b.bind("localhost", 8025).sync().channel(); ClientWebSocketDuplexConnection duplexConnection = RxReactiveStreams .toObservable(ClientWebSocketDuplexConnection.create( InetSocketAddress.createUnresolved("localhost", 8025), "/rs", new NioEventLoopGroup())) .toBlocking().single(); client = DefaultReactiveSocket.fromClientConnection(duplexConnection, ConnectionSetupPayload.create("UTF-8", "UTF-8"), t -> t.printStackTrace()); Unsafe.startAndWait(client); }
From source file:io.reactivesocket.transport.websocket.Pong.java
License:Apache License
public static void main(String... args) throws Exception { byte[] response = new byte[1024]; Random r = new Random(); r.nextBytes(response);/*from w w w . j a v a 2 s . c om*/ RequestHandler requestHandler = new RequestHandler() { @Override public Publisher<Payload> handleRequestResponse(Payload payload) { return subscriber -> { Payload responsePayload = new Payload() { ByteBuffer data = ByteBuffer.wrap(response); ByteBuffer metadata = ByteBuffer.allocate(0); public ByteBuffer getData() { return data; } @Override public ByteBuffer getMetadata() { return metadata; } }; subscriber.onNext(responsePayload); subscriber.onComplete(); }; } @Override public Publisher<Payload> handleRequestStream(Payload payload) { Payload response = TestUtil.utf8EncodedPayload("hello world", "metadata"); return RxReactiveStreams.toPublisher(Observable.range(1, 10).map(i -> response)); } @Override public Publisher<Payload> handleSubscription(Payload payload) { Payload response = TestUtil.utf8EncodedPayload("hello world", "metadata"); return RxReactiveStreams.toPublisher(Observable.range(1, 10).map(i -> response)); } @Override public Publisher<Void> handleFireAndForget(Payload payload) { return Subscriber::onComplete; } @Override public Publisher<Payload> handleChannel(Payload initialPayload, Publisher<Payload> inputs) { Observable<Payload> observable = RxReactiveStreams.toObservable(inputs).map(input -> input); return RxReactiveStreams.toPublisher(observable); } @Override public Publisher<Void> handleMetadataPush(Payload payload) { return null; } }; EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(64 * 1024)); pipeline.addLast(new WebSocketServerProtocolHandler("/rs")); ReactiveSocketServerHandler serverHandler = ReactiveSocketServerHandler .create((setupPayload, rs) -> requestHandler); pipeline.addLast(serverHandler); } }); Channel localhost = b.bind("localhost", 8025).sync().channel(); localhost.closeFuture().sync(); }