@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (e.getMessage() instanceof WebSocketFrame) { WebSocketFrame frame = (WebSocketFrame) e.getMessage(); if (frame instanceof CloseWebSocketFrame) { WebSocketServerHandshaker handshaker = getHandshaker(ctx); handshaker.close(ctx.getChannel(), (CloseWebSocketFrame) frame); return; } else if (frame instanceof PingWebSocketFrame) { ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData())); return; } } ctx.sendUpstream(e); }
@Override public void messageReceived(final ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (e.getMessage() instanceof HttpRequest) { HttpRequest req = (HttpRequest) e.getMessage(); if (req.getMethod() != GET) { sendHttpResponse(ctx, req, new DefaultHttpResponse(HTTP_1_1, FORBIDDEN)); return; } final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory( getWebSocketLocation(ctx.getPipeline(), req, websocketPath), subprotocols, allowExtensions); final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel()); } else { final ChannelFuture handshakeFuture = handshaker.handshake(ctx.getChannel(), req); handshakeFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { Channels.fireExceptionCaught(ctx, future.getCause()); } } }); WebSocketServerProtocolHandler.setHandshaker(ctx, handshaker); ctx.getPipeline().replace(this, "WS403Responder", WebSocketServerProtocolHandler.forbiddenHttpRequestResponder()); } } }
private void handleWebSocketFrame(ChannelHandlerContext ctx, Channel channel, WebSocketFrame frame) throws IOException { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.getChannel(), (CloseWebSocketFrame) frame); return; } else if (frame instanceof PingWebSocketFrame) { ctx.getChannel().write(new PongWebSocketFrame(frame.getBinaryData())); return; } else if (!(frame instanceof TextWebSocketFrame)) { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } String request = ((TextWebSocketFrame) frame).getText(); logger.debug(String.format("Channel %s received '%s'", ctx.getChannel().getId(), request)); SockJsMessage jsMessage = new SockJsMessage(request); ctx.sendUpstream(new UpstreamMessageEvent(channel, jsMessage, channel.getRemoteAddress())); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { HttpRequest request = (HttpRequest) e.getMessage(); if (request.getUri().startsWith(WEBSOCKET_PATH)) { // Websocket handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(request), null, false); handshaker = wsFactory.newHandshaker(request); if (handshaker == null) { wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel()); } else { handshaker.handshake(ctx.getChannel(), request).addListener(WebSocketServerHandshaker.HANDSHAKE_LISTENER); } // extract topic from request URI String topic = request.getUri(); if (topic.length() > WEBSOCKET_PATH.length() + 1) { topic = topic.substring(WEBSOCKET_PATH.length() + 1); serverTransport.presence(Presence.CONNECTED, topic, e.getChannel()); } } else { NettyHttpRequest nettyHttpRequest =new NettyHttpRequest(request); serverTransport.dispatchRequest(nettyHttpRequest, new NettyHttpChannel(serverTransport, e.getChannel(), nettyHttpRequest)); super.messageReceived(ctx, e); } }
public void frame(WebSocketServerHandshaker handshaker, WebSocketFrame frame, ChannelHandlerContext context) { Channel channel = context.getChannel(); if (frame instanceof TextWebSocketFrame) { text((TextWebSocketFrame) frame, channel); } else if (handshaker != null && frame instanceof CloseWebSocketFrame) { handshaker.close(context.getChannel(), (CloseWebSocketFrame) frame); presence(Presence.DISCONNECTED, null, channel); } else if (frame instanceof PingWebSocketFrame) { channel.write(new PongWebSocketFrame(frame.getBinaryData())); } }
} else { try { this.handshaker.handshake(ctx.getChannel(), req); } catch (Exception e) { e.printStackTrace();
private void websocketFrameReceived(ChannelHandlerContext ctx, WebSocketFrame webSocketFrame) { Http.Inbound inbound = channels.get(ctx); // Check for closing frame if (webSocketFrame instanceof CloseWebSocketFrame) { this.handshaker.close(ctx.getChannel(), (CloseWebSocketFrame) webSocketFrame); } else if (webSocketFrame instanceof PingWebSocketFrame) { ctx.getChannel().write(new PongWebSocketFrame(webSocketFrame.getBinaryData())); } else if (webSocketFrame instanceof BinaryWebSocketFrame) { inbound._received(new Http.WebSocketFrame(webSocketFrame.getBinaryData().array())); } else if (webSocketFrame instanceof TextWebSocketFrame) { inbound._received(new Http.WebSocketFrame(((TextWebSocketFrame) webSocketFrame).getText())); } }
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel()); } else { handshaker.handshake(ctx.getChannel(), req).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception {
private void handleWebSocketFrame(ChannelHandlerContext ctx, Channel channel, WebSocketFrame frame) throws IOException { handshaker.close(ctx.getChannel(), (CloseWebSocketFrame) frame); return; } else if (frame instanceof PingWebSocketFrame) {
private void handleHttpRequest(final ChannelHandlerContext ctx, final Channel channel, HttpRequest req) throws Exception { // Allow only GET methods. if (req.getMethod() != GET) { DefaultHttpResponse response = new DefaultHttpResponse(HTTP_1_1, METHOD_NOT_ALLOWED); response.addHeader(ALLOW, GET.toString()); sendHttpResponse(ctx, req, response); return; } // Handshake WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), "chat, superchat", false); handshaker = wsFactory.newHandshaker(req); if (handshaker == null) { wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.getChannel()); } else { handshaker.handshake(ctx.getChannel(), req).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { ctx.getPipeline().remove(ServiceRouter.class); ctx.getPipeline().remove(PreflightHandler.class); ctx.sendUpstream(new UpstreamChannelStateEvent(channel, ChannelState.CONNECTED, Boolean.TRUE)); } } }); } }