Search in sources :

Example 1 with ClientHead

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

the class PollingTransport method onGet.

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);
        return;
    }
    client.bindChannel(ctx.channel(), Transport.POLLING);
    authorizeHandler.connect(client);
}
Also used : ClientHead(com.corundumstudio.socketio.handler.ClientHead)

Example 2 with ClientHead

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

the class PollingTransport method onPost.

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);
        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));
}
Also used : XHRPostMessage(com.corundumstudio.socketio.messages.XHRPostMessage) PacketsMessage(com.corundumstudio.socketio.messages.PacketsMessage) ClientHead(com.corundumstudio.socketio.handler.ClientHead)

Example 3 with ClientHead

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

the class PollingTransport method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof FullHttpRequest) {
        FullHttpRequest req = (FullHttpRequest) msg;
        QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
        List<String> transport = queryDecoder.parameters().get("transport");
        if (transport != null && NAME.equals(transport.get(0))) {
            List<String> sid = queryDecoder.parameters().get("sid");
            List<String> j = queryDecoder.parameters().get("j");
            List<String> b64 = queryDecoder.parameters().get("b64");
            String origin = req.headers().get(HttpHeaderNames.ORIGIN);
            ctx.channel().attr(EncoderHandler.ORIGIN).set(origin);
            String userAgent = req.headers().get(HttpHeaderNames.USER_AGENT);
            ctx.channel().attr(EncoderHandler.USER_AGENT).set(userAgent);
            if (j != null && j.get(0) != null) {
                Integer index = Integer.valueOf(j.get(0));
                ctx.channel().attr(EncoderHandler.JSONP_INDEX).set(index);
            }
            if (b64 != null && b64.get(0) != null) {
                Integer enable = Integer.valueOf(b64.get(0));
                ctx.channel().attr(EncoderHandler.B64).set(enable == 1);
            }
            try {
                if (sid != null && sid.get(0) != null) {
                    final UUID sessionId = UUID.fromString(sid.get(0));
                    handleMessage(req, sessionId, queryDecoder, ctx);
                } else {
                    // first connection
                    ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
                    handleMessage(req, client.getSessionId(), queryDecoder, ctx);
                }
            } finally {
                req.release();
            }
            return;
        }
    }
    ctx.fireChannelRead(msg);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ClientHead(com.corundumstudio.socketio.handler.ClientHead) UUID(java.util.UUID)

Example 4 with ClientHead

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

the class PollingTransport method handleMessage.

private void handleMessage(FullHttpRequest req, UUID sessionId, QueryStringDecoder queryDecoder, ChannelHandlerContext ctx) throws IOException {
    String origin = req.headers().get(HttpHeaderNames.ORIGIN);
    if (queryDecoder.parameters().containsKey("disconnect")) {
        ClientHead client = clientsBox.get(sessionId);
        client.onChannelDisconnect();
        ctx.channel().writeAndFlush(new XHRPostMessage(origin, sessionId));
    } else if (HttpMethod.POST.equals(req.method())) {
        onPost(sessionId, ctx, origin, req.content());
    } else if (HttpMethod.GET.equals(req.method())) {
        onGet(sessionId, ctx, origin);
    } else if (HttpMethod.OPTIONS.equals(req.method())) {
        onOptions(sessionId, ctx, origin);
    } else {
        log.error("Wrong {} method invocation for {}", req.method(), sessionId);
        sendError(ctx);
    }
}
Also used : XHRPostMessage(com.corundumstudio.socketio.messages.XHRPostMessage) ClientHead(com.corundumstudio.socketio.handler.ClientHead)

Example 5 with ClientHead

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

the class PollingTransport method onOptions.

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);
        return;
    }
    ctx.channel().writeAndFlush(new XHROptionsMessage(origin, sessionId));
}
Also used : XHROptionsMessage(com.corundumstudio.socketio.messages.XHROptionsMessage) ClientHead(com.corundumstudio.socketio.handler.ClientHead)

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