Search in sources :

Example 31 with Packet

use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.

the class PayloadTest method testPayloadEncode.

@Test
public void testPayloadEncode() throws IOException {
    Queue<Packet> packets = new ConcurrentLinkedQueue<Packet>();
    Packet packet1 = new Packet(PacketType.MESSAGE);
    packet1.setData("5");
    packets.add(packet1);
    Packet packet2 = new Packet(PacketType.MESSAGE);
    packet2.setData("53d");
    packets.add(packet2);
    ByteBuf result = Unpooled.buffer();
    //        encoder.encodePackets(packets, result, UnpooledByteBufAllocator.DEFAULT);
    Assert.assertEquals("�5�3:::5�7�3:::53d", result.toString(CharsetUtil.UTF_8));
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 32 with Packet

use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.

the class AuthorizeHandler method connect.

public void connect(ClientHead client) {
    Namespace ns = namespacesHub.get(Namespace.DEFAULT_NAME);
    if (!client.getNamespaces().contains(ns)) {
        Packet packet = new Packet(PacketType.MESSAGE);
        packet.setSubType(PacketType.CONNECT);
        client.send(packet);
        configuration.getStoreFactory().pubSubStore().publish(PubSubType.CONNECT, new ConnectMessage(client.getSessionId()));
        SocketIOClient nsClient = client.addNamespaceClient(ns);
        ns.onConnect(nsClient);
    }
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) AuthPacket(com.corundumstudio.socketio.protocol.AuthPacket) SocketIOClient(com.corundumstudio.socketio.SocketIOClient) ConnectMessage(com.corundumstudio.socketio.store.pubsub.ConnectMessage) Namespace(com.corundumstudio.socketio.namespace.Namespace)

Example 33 with Packet

use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.

the class ClientHead method disconnect.

public void disconnect() {
    ChannelFuture future = send(new Packet(PacketType.DISCONNECT));
    future.addListener(ChannelFutureListener.CLOSE);
    onChannelDisconnect();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Packet(com.corundumstudio.socketio.protocol.Packet)

Example 34 with Packet

use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.

the class EncoderHandler method handleHTTP.

private void handleHTTP(OutPacketMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) throws IOException {
    Channel channel = ctx.channel();
    Attribute<Boolean> attr = channel.attr(WRITE_ONCE);
    Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport());
    if (!channel.isActive() || queue.isEmpty() || !attr.compareAndSet(null, true)) {
        promise.trySuccess();
        return;
    }
    ByteBuf out = encoder.allocateBuffer(ctx.alloc());
    Boolean b64 = ctx.channel().attr(EncoderHandler.B64).get();
    if (b64 != null && b64) {
        Integer jsonpIndex = ctx.channel().attr(EncoderHandler.JSONP_INDEX).get();
        encoder.encodeJsonP(jsonpIndex, queue, out, ctx.alloc(), 50);
        String type = "application/javascript";
        if (jsonpIndex == null) {
            type = "text/plain";
        }
        sendMessage(msg, channel, out, type, promise, HttpResponseStatus.OK);
    } else {
        encoder.encodePackets(queue, out, ctx.alloc(), 50);
        sendMessage(msg, channel, out, "application/octet-stream", promise, HttpResponseStatus.OK);
    }
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf)

Example 35 with Packet

use of com.corundumstudio.socketio.protocol.Packet in project netty-socketio by mrniko.

the class EncoderHandler method handleWebsocket.

private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) throws IOException {
    while (true) {
        Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport());
        Packet packet = queue.poll();
        if (packet == null) {
            promise.trySuccess();
            break;
        }
        final ByteBuf out = encoder.allocateBuffer(ctx.alloc());
        encoder.encodePacket(packet, out, ctx.alloc(), true);
        WebSocketFrame res = new TextWebSocketFrame(out);
        if (log.isTraceEnabled()) {
            log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId());
        }
        if (out.isReadable()) {
            if (!promise.isDone()) {
                ctx.channel().writeAndFlush(res, promise);
            } else {
                ctx.channel().writeAndFlush(res);
            }
        } else {
            promise.trySuccess();
            out.release();
        }
        for (ByteBuf buf : packet.getAttachments()) {
            ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc());
            outBuf.writeByte(4);
            outBuf.writeBytes(buf);
            if (log.isTraceEnabled()) {
                log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId());
            }
            ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf));
        }
    }
}
Also used : Packet(com.corundumstudio.socketio.protocol.Packet) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) WebSocketFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

Packet (com.corundumstudio.socketio.protocol.Packet)39 Test (org.junit.Test)27 ByteBuf (io.netty.buffer.ByteBuf)15 Namespace (com.corundumstudio.socketio.namespace.Namespace)2 AuthPacket (com.corundumstudio.socketio.protocol.AuthPacket)2 HandshakeData (com.corundumstudio.socketio.HandshakeData)1 SocketIOClient (com.corundumstudio.socketio.SocketIOClient)1 Transport (com.corundumstudio.socketio.Transport)1 HttpErrorMessage (com.corundumstudio.socketio.messages.HttpErrorMessage)1 JacksonJsonSupport (com.corundumstudio.socketio.protocol.JacksonJsonSupport)1 PacketDecoder (com.corundumstudio.socketio.protocol.PacketDecoder)1 ConnectMessage (com.corundumstudio.socketio.store.pubsub.ConnectMessage)1 NamespaceClient (com.corundumstudio.socketio.transport.NamespaceClient)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 BinaryWebSocketFrame (io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame)1 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)1 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)1