Search in sources :

Example 1 with WSConfig

use of io.transport.core.config.Configuration.WSConfig 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 2 with WSConfig

use of io.transport.core.config.Configuration.WSConfig in project transporter by wang4ever.

the class TextWSFrameHandler method processHttpRequest.

/**
 * 处理http的握手请求
 *
 * @param ctx
 * @param req
 * @throws Exception
 */
private void processHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    // 如果HTTP解码失败则返回HTTP异常, 若不是WebSocket则直接异常返回
    if (!req.decoderResult().isSuccess() || (!"websocket".equals(req.headers().get("Upgrade")))) {
        this.sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;
    }
    // 构造握手响应返回
    String hostAndPort = ctx.channel().localAddress().toString();
    WSConfig conf = this.config.getWsConfig();
    String wspath = (conf.getSslEnable() ? "wss:/" : "ws:/") + hostAndPort + conf.getWebsocketPath();
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(wspath, null, conf.getAllowExtensions());
    this.handshaker = wsFactory.newHandshaker(req);
    if (this.handshaker == null)
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    else
        // 返回握手response
        this.handshaker.handshake(ctx.channel(), req);
}
Also used : DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) WSConfig(io.transport.core.config.Configuration.WSConfig)

Aggregations

WSConfig (io.transport.core.config.Configuration.WSConfig)2 ChannelPipeline (io.netty.channel.ChannelPipeline)1 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)1 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)1 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)1 WebSocketServerProtocolHandler (io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler)1 LoggingHandler (io.netty.handler.logging.LoggingHandler)1 SslContext (io.netty.handler.ssl.SslContext)1 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1 File (java.io.File)1