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));
}
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);
}
}
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();
}
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);
}
}
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));
}
}
}
Aggregations