Search in sources :

Example 6 with ClientHead

use of com.corundumstudio.socketio.handler.ClientHead in project netty-socketio by mrniko.

the class WebSocketTransport method channelRead.

@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();
            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.uri());
        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);
    }
}
Also used : CloseWebSocketFrame(io.netty.handler.codec.http.websocketx.CloseWebSocketFrame) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) PacketsMessage(com.corundumstudio.socketio.messages.PacketsMessage) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) ByteBufHolder(io.netty.buffer.ByteBufHolder) ClientHead(com.corundumstudio.socketio.handler.ClientHead) UUID(java.util.UUID)

Example 7 with ClientHead

use of com.corundumstudio.socketio.handler.ClientHead in project netty-socketio by mrniko.

the class WebSocketTransport method channelInactive.

@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();
    }
    super.channelInactive(ctx);
}
Also used : ClientHead(com.corundumstudio.socketio.handler.ClientHead)

Example 8 with ClientHead

use of com.corundumstudio.socketio.handler.ClientHead in project netty-socketio by mrniko.

the class WebSocketTransport method connectClient.

private void connectClient(final Channel channel, final UUID sessionId) {
    ClientHead client = clientsBox.get(sessionId);
    if (client == null) {
        log.warn("Unauthorized client with sessionId: {} with ip: {}. Channel closed!", sessionId, channel.remoteAddress());
        channel.close();
        return;
    }
    client.bindChannel(channel, Transport.WEBSOCKET);
    authorizeHandler.connect(client);
    if (client.getCurrentTransport() == Transport.POLLING) {
        SchedulerKey key = new SchedulerKey(SchedulerKey.Type.UPGRADE_TIMEOUT, sessionId);
        scheduler.schedule(key, new Runnable() {

            @Override
            public void run() {
                ClientHead clientHead = clientsBox.get(sessionId);
                if (clientHead != null) {
                    if (log.isDebugEnabled()) {
                        log.debug("client did not complete upgrade - closing transport");
                    }
                    clientHead.onChannelDisconnect();
                }
            }
        }, configuration.getUpgradeTimeout(), TimeUnit.MILLISECONDS);
    }
    log.debug("сlient {} handshake completed", sessionId);
}
Also used : ClientHead(com.corundumstudio.socketio.handler.ClientHead) SchedulerKey(com.corundumstudio.socketio.scheduler.SchedulerKey)

Aggregations

ClientHead (com.corundumstudio.socketio.handler.ClientHead)8 PacketsMessage (com.corundumstudio.socketio.messages.PacketsMessage)2 XHRPostMessage (com.corundumstudio.socketio.messages.XHRPostMessage)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)2 UUID (java.util.UUID)2 XHROptionsMessage (com.corundumstudio.socketio.messages.XHROptionsMessage)1 SchedulerKey (com.corundumstudio.socketio.scheduler.SchedulerKey)1 ByteBufHolder (io.netty.buffer.ByteBufHolder)1 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)1 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1