Search in sources :

Example 1 with WebSocketServerProtocolHandler

use of io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler in project java-in-action by xinghalo.

the class ChatServerInitializer method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new ChunkedWriteHandler());
    pipeline.addLast(new HttpObjectAggregator(60 * 1024));
    pipeline.addLast(new HttpRequestHandler("/ws"));
    pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));
    pipeline.addLast(new TextWebSocketFrameHandler(group));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 2 with WebSocketServerProtocolHandler

use of io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler in project transporter by wang4ever.

the class WSChildHandlerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    WSConfig conf = this.config.getWsConfig();
    if (logger.isDebugEnabled())
        logger.debug("Initital channel handler...\twebsocketPath={}", conf.getWebsocketPath());
    ChannelPipeline p = ch.pipeline();
    // Configure SSL.
    if (conf.getSslEnable()) {
        /*
			 * SelfSignedCertificate ssc = new SelfSignedCertificate();
			 * SslContext sslCtx =
			 * SslContextBuilder.forServer(ssc.certificate(),
			 * ssc.privateKey()).build();
			 */
        File keyCertChainFile = new File(Resources.getResource(conf.getKeyCertChainFile()).getFile());
        File keyFile = new File(Resources.getResource(conf.getKeyFile()).getFile());
        SslContext sslCtx = SslContextBuilder.forServer(keyCertChainFile, keyFile).build();
        p.addLast(sslCtx.newHandler(ch.alloc()));
        if (logger.isDebugEnabled())
            logger.debug("The ssl(wss) handler has been enabled. sslCtx={}", sslCtx.toString());
    }
    // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务
    if (conf.getLoggingEnable()) {
        p.addLast(new LoggingHandler(LogLevel.valueOf(conf.getLoggingLevel())));
        if (logger.isInfoEnabled())
            logger.info("Netty internal log has been used. (WS)level={}", conf.getLoggingLevel());
    }
    IdleStateHandler idleHandler = new IdleStateHandler(conf.getReadIdleSeconds(), conf.getWriteIdleSeconds(), conf.getAllIdleSeconds());
    p.addLast(idleHandler);
    // 将请求和应答消息解码为HTTP消息
    p.addLast("http-codec", new HttpServerCodec());
    // 将HTTP消息的多个部分合成一条完整的HTTP消息
    p.addLast("aggregator", new HttpObjectAggregator(65536));
    // 向客户端发送HTML5文件
    p.addLast("http-chunked", new ChunkedWriteHandler());
    p.addLast("ws-protocol", new WebSocketServerProtocolHandler(conf.getWebsocketPath(), null, true));
    p.addLast("text-handler", SpringContextHolder.getBean("textWSFrameHandler"));
}
Also used : WSConfig(io.transport.core.config.Configuration.WSConfig) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) LoggingHandler(io.netty.handler.logging.LoggingHandler) ChunkedWriteHandler(io.netty.handler.stream.ChunkedWriteHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) File(java.io.File) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslContext(io.netty.handler.ssl.SslContext)

Example 3 with WebSocketServerProtocolHandler

use of io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler in project ballerina by ballerina-lang.

the class WebSocketRemoteServerInitializer method initChannel.

@Override
public void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addLast(sslCtx.newHandler(ch.alloc()));
    }
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerCompressionHandler());
    pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
    WebSocketRemoteServerFrameHandler frameHandler = new WebSocketRemoteServerFrameHandler();
    FRAME_HANDLERS.add(frameHandler);
    pipeline.addLast(frameHandler);
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) WebSocketServerCompressionHandler(io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 4 with WebSocketServerProtocolHandler

use of io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler in project neo4j by neo4j.

the class TransportSelectionHandler method switchToWebsocket.

private void switchToWebsocket(ChannelHandlerContext ctx) {
    ChannelPipeline p = ctx.pipeline();
    memoryTracker.allocateHeap(HTTP_SERVER_CODEC_SHALLOW_SIZE + HTTP_OBJECT_AGGREGATOR_SHALLOW_SIZE + WEB_SOCKET_SERVER_PROTOCOL_HANDLER_SHALLOW_SIZE + WEB_SOCKET_FRAME_AGGREGATOR_SHALLOW_SIZE + WebSocketFrameTranslator.SHALLOW_SIZE + ProtocolHandshaker.SHALLOW_SIZE);
    p.addLast(new HttpServerCodec(), new HttpObjectAggregator(MAX_WEBSOCKET_HANDSHAKE_SIZE), new WebSocketServerProtocolHandler("/", null, false, MAX_WEBSOCKET_FRAME_SIZE), new WebSocketFrameAggregator(MAX_WEBSOCKET_FRAME_SIZE), new WebSocketFrameTranslator(), newHandshaker());
    p.remove(this);
}
Also used : WebSocketFrameAggregator(io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) WebSocketFrameTranslator(org.neo4j.bolt.transport.pipeline.WebSocketFrameTranslator) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 5 with WebSocketServerProtocolHandler

use of io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler in project summer by foxsugar.

the class WebSocketServerInitializer method initChannel.

@Override
protected void initChannel(SocketChannel ch) throws Exception {
    ChannelPipeline pipeline = ch.pipeline();
    // 处理日志
    // pipeline.addLast(new LoggingHandler(LogLevel.INFO));
    // 处理心跳
    // pipeline.addLast(new IdleStateHandler(15, 0, 0, TimeUnit.SECONDS));
    // pipeline.addLast(new ChatHeartbeatHandler());
    // // 获取职责链
    // pipeline.addLast("http-codec", new HttpServerCodec());
    // pipeline.addLast("aggregator", new HttpObjectAggregator(64*1024));
    // pipeline.addLast("http-chunked", new ChunkedWriteHandler());
    // //        pipeline.addLast(new WebSocketServerCompressionHandler());
    // 
    // //        pipeline.addLast("encoder", new WsEncoder());
    // //        pipeline.addLast("decoder", new WsDecoder());
    // pipeline.addLast("handshake",new WebSocketServerProtocolHandler("/ws"));
    // pipeline.addLast(new WsCodec());
    // //        pipeline.addLast(new WsGameMsgHandler());
    // pipeline.addLast(new WsHandler());
    // //        pipeline.addLast("codec", new WsEncoder());
    // //websocket定义了传递数据的6中frame类型
    // //        pipeline.addLast(new GameMsgHandler());
    // 
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(64 * 1024));
    pipeline.addLast(new WebSocketServerProtocolHandler("/websocket", null, true));
    pipeline.addLast(new WsCodec());
    pipeline.addLast(new GameMsgHandler());
// ChannelPipeline pipeline = ch.pipeline();
// pipeline.addLast(new HttpServerCodec());
// pipeline.addLast(new HttpObjectAggregator(65536));
// //        pipeline.addLast(new WebSocketServerCompressionHandler());
// //        pipeline.addLast(new WebSocketServerProtocolHandler(WEBSOCKET_PATH, null, true));
// //        pipeline.addLast(new WebSocketIndexPageHandler(WEBSOCKET_PATH));
// pipeline.addLast(new WebSocketServerHandler());
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) GameMsgHandler(com.code.server.gate.handle.GameMsgHandler) WsCodec(com.code.server.gate.encoding.WsCodec) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) WebSocketServerProtocolHandler(io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler) ChannelPipeline(io.netty.channel.ChannelPipeline)

Aggregations

WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)8 ChannelPipeline (io.netty.channel.ChannelPipeline)7 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)7 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)7 WebSocketServerCompressionHandler (io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketServerCompressionHandler)3 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)3 WsCodec (com.code.server.gate.encoding.WsCodec)1 GameMsgHandler (com.code.server.gate.handle.GameMsgHandler)1 WebSocketFrameAggregator (io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1 SslContext (io.netty.handler.ssl.SslContext)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 WSConfig (io.transport.core.config.Configuration.WSConfig)1 File (java.io.File)1 WebSocketFrameTranslator (org.neo4j.bolt.transport.pipeline.WebSocketFrameTranslator)1