List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:com.corundumstudio.socketio.transport.PollingTransport.java
License:Apache License
private void onOptions(UUID sessionId, ChannelHandlerContext ctx, String origin) { ClientHead client = clientsBox.get(sessionId); if (client == null) { log.error("{} is not registered. Closing connection", sessionId); sendError(ctx);//from www . j a v a 2s .co m return; } ctx.channel().writeAndFlush(new XHROptionsMessage(origin, sessionId)); }
From source file:com.corundumstudio.socketio.transport.PollingTransport.java
License:Apache License
private void onPost(UUID sessionId, ChannelHandlerContext ctx, String origin, ByteBuf content) throws IOException { ClientHead client = clientsBox.get(sessionId); if (client == null) { log.error("{} is not registered. Closing connection", sessionId); sendError(ctx);/*from w w w . j a v a 2s . c om*/ return; } // release POST response before message processing ctx.channel().writeAndFlush(new XHRPostMessage(origin, sessionId)); Boolean b64 = ctx.channel().attr(EncoderHandler.B64).get(); if (b64 != null && b64) { Integer jsonIndex = ctx.channel().attr(EncoderHandler.JSONP_INDEX).get(); content = decoder.preprocessJson(jsonIndex, content); } ctx.pipeline().fireChannelRead(new PacketsMessage(client, content, Transport.POLLING)); }
From source file:com.corundumstudio.socketio.transport.PollingTransport.java
License:Apache License
protected void onGet(UUID sessionId, ChannelHandlerContext ctx, String origin) { ClientHead client = clientsBox.get(sessionId); if (client == null) { log.error("{} is not registered. Closing connection", sessionId); sendError(ctx);/*from w ww.j a v a2 s . c o m*/ return; } client.bindChannel(ctx.channel(), Transport.POLLING); authorizeHandler.connect(client); }
From source file:com.corundumstudio.socketio.transport.PollingTransport.java
License:Apache License
private void sendError(ChannelHandlerContext ctx) { HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR); ctx.channel().writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); }
From source file:com.corundumstudio.socketio.transport.WebSocketTransport.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof CloseWebSocketFrame) { ctx.channel().close(); ReferenceCountUtil.release(msg); } else if (msg instanceof BinaryWebSocketFrame || msg instanceof TextWebSocketFrame) { ByteBufHolder frame = (ByteBufHolder) msg; ClientHead client = clientsBox.get(ctx.channel()); if (client == null) { log.debug("Client with was already disconnected. Channel closed!"); ctx.channel().close();//from www .jav a2 s . c om frame.release(); return; } ctx.pipeline().fireChannelRead(new PacketsMessage(client, frame.content(), Transport.WEBSOCKET)); frame.release(); } else if (msg instanceof FullHttpRequest) { FullHttpRequest req = (FullHttpRequest) msg; QueryStringDecoder queryDecoder = new QueryStringDecoder(req.getUri()); String path = queryDecoder.path(); List<String> transport = queryDecoder.parameters().get("transport"); List<String> sid = queryDecoder.parameters().get("sid"); if (transport != null && NAME.equals(transport.get(0))) { try { if (!configuration.getTransports().contains(Transport.WEBSOCKET)) { log.debug("{} transport not supported by configuration.", Transport.WEBSOCKET); ctx.channel().close(); return; } if (sid != null && sid.get(0) != null) { final UUID sessionId = UUID.fromString(sid.get(0)); handshake(ctx, sessionId, path, req); } else { ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get(); // first connection handshake(ctx, client.getSessionId(), path, req); } } finally { req.release(); } } else { ctx.fireChannelRead(msg); } } else { ctx.fireChannelRead(msg); } }
From source file:com.corundumstudio.socketio.transport.WebSocketTransport.java
License:Apache License
@Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ClientHead client = clientsBox.get(ctx.channel()); if (client != null && client.isTransportChannel(ctx.channel(), Transport.WEBSOCKET)) { ctx.flush();//from ww w. j a va2s. c om } else { super.channelReadComplete(ctx); } }
From source file:com.corundumstudio.socketio.transport.WebSocketTransport.java
License:Apache License
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { ClientHead client = clientsBox.get(ctx.channel()); if (client != null && client.isTransportChannel(ctx.channel(), Transport.WEBSOCKET)) { log.debug("channel inactive {}", client.getSessionId()); client.onChannelDisconnect();//from w ww . ja va 2s .c om } super.channelInactive(ctx); }
From source file:com.corundumstudio.socketio.transport.WebSocketTransport.java
License:Apache License
private void handshake(ChannelHandlerContext ctx, final UUID sessionId, String path, FullHttpRequest req) { final Channel channel = ctx.channel(); WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req), null, false, configuration.getMaxFramePayloadLength()); WebSocketServerHandshaker handshaker = factory.newHandshaker(req); if (handshaker != null) { ChannelFuture f = handshaker.handshake(channel, req); f.addListener(new ChannelFutureListener() { @Override/* w ww. j a v a 2 s.c o m*/ 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()); } }
From source file:com.corundumstudio.socketio.transport.XHRPollingTransport.java
License:Apache License
private void handleMessage(FullHttpRequest req, QueryStringDecoder queryDecoder, ChannelHandlerContext ctx) throws IOException { String[] parts = queryDecoder.path().split("/"); if (parts.length > 3) { UUID sessionId = UUID.fromString(parts[4]); String origin = req.headers().get(HttpHeaders.Names.ORIGIN); if (queryDecoder.parameters().containsKey("disconnect")) { MainBaseClient client = sessionId2Client.get(sessionId); client.onChannelDisconnect(); ctx.channel().writeAndFlush(new XHROutMessage(origin, sessionId)); } else if (HttpMethod.POST.equals(req.getMethod())) { onPost(sessionId, ctx, origin, req.content()); } else if (HttpMethod.GET.equals(req.getMethod())) { onGet(sessionId, ctx, origin); }//from w w w. j av a 2s. c o m } else { log.warn("Wrong {} method request path: {}, from ip: {}. Channel closed!", req.getMethod(), path, ctx.channel().remoteAddress()); ctx.channel().close(); } }
From source file:com.corundumstudio.socketio.transport.XHRPollingTransport.java
License:Apache License
private void onPost(UUID sessionId, ChannelHandlerContext ctx, String origin, ByteBuf content) throws IOException { XHRPollingClient client = sessionId2Client.get(sessionId); if (client == null) { log.debug("Client with sessionId: {} was already disconnected. Channel closed!", sessionId); ctx.channel().close(); return;/*from w w w. j a v a 2s.c om*/ } // release POST response before message processing ctx.channel().writeAndFlush(new XHROutMessage(origin, sessionId)); ctx.pipeline().fireChannelRead(new PacketsMessage(client, content)); }