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"));
}
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);
}
Aggregations