use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker in project netty-socketio by mrniko.
the class WebSocketTransport method handshake.
private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String path, FullHttpRequest req) {
final Channel channel = ctx.channel();
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, true, configuration.getMaxFramePayloadLength());
WebSocketServerHandshaker handshaker = factory.newHandshaker(req);
if (handshaker != null) {
ChannelFuture f = handshaker.handshake(channel, req);
f.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (!future.isSuccess()) {
log.error("Can't handshake " + sessionId, future.cause());
return;
}
channel.pipeline().addBefore(SocketIOChannelInitializer.WEB_SOCKET_TRANSPORT, SocketIOChannelInitializer.WEB_SOCKET_AGGREGATOR, new WebSocketFrameAggregator(configuration.getMaxFramePayloadLength()));
connectClient(channel, sessionId);
}
});
} else {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
}
}
use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker in project intellij-community by JetBrains.
the class WebSocketHandshakeHandler method handleWebSocketRequest.
private void handleWebSocketRequest(@NotNull final ChannelHandlerContext context, @NotNull FullHttpRequest request, @NotNull final QueryStringDecoder uriDecoder) {
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory("ws://" + request.headers().getAsString(HttpHeaderNames.HOST) + uriDecoder.path(), null, false, NettyUtil.MAX_CONTENT_LENGTH);
WebSocketServerHandshaker handshaker = factory.newHandshaker(request);
if (handshaker == null) {
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(context.channel());
return;
}
if (!context.channel().isOpen()) {
return;
}
final Client client = new WebSocketClient(context.channel(), handshaker);
context.channel().attr(ClientManagerKt.getCLIENT()).set(client);
handshaker.handshake(context.channel(), request).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
ClientManager clientManager = WebSocketHandshakeHandler.this.clientManager.getValue();
clientManager.addClient(client);
MessageChannelHandler messageChannelHandler = new MessageChannelHandler(clientManager, getMessageServer());
BuiltInServer.replaceDefaultHandler(context, messageChannelHandler);
ChannelHandlerContext messageChannelHandlerContext = context.pipeline().context(messageChannelHandler);
context.pipeline().addBefore(messageChannelHandlerContext.name(), "webSocketFrameAggregator", new WebSocketFrameAggregator(NettyUtil.MAX_CONTENT_LENGTH));
messageChannelHandlerContext.channel().attr(ClientManagerKt.getCLIENT()).set(client);
connected(client, uriDecoder.parameters());
}
}
});
}
use of io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker in project vert.x by eclipse.
the class HttpServerImpl method createHandshaker.
WebSocketServerHandshaker createHandshaker(Channel ch, HttpRequest request) {
// As a fun part, Firefox 6.0.2 supports Websockets protocol '7'. But,
// it doesn't send a normal 'Connection: Upgrade' header. Instead it
// sends: 'Connection: keep-alive, Upgrade'. Brilliant.
String connectionHeader = request.headers().get(io.vertx.core.http.HttpHeaders.CONNECTION);
if (connectionHeader == null || !connectionHeader.toLowerCase().contains("upgrade")) {
sendError("\"Connection\" must be \"Upgrade\".", BAD_REQUEST, ch);
return null;
}
if (request.getMethod() != HttpMethod.GET) {
sendError(null, METHOD_NOT_ALLOWED, ch);
return null;
}
try {
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(ch.pipeline(), request), subProtocols, false, options.getMaxWebsocketFrameSize(), options.isAcceptUnmaskedFrames());
WebSocketServerHandshaker shake = factory.newHandshaker(request);
if (shake == null) {
log.error("Unrecognised websockets handshake");
WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ch);
}
return shake;
} catch (Exception e) {
throw new VertxException(e);
}
}
Aggregations