buf.writeBytes(packet.getData().toString().getBytes(CharsetUtil.UTF_8));
break;
}
case OPEN: {
ByteBufOutputStream out = new ByteBufOutputStream(buf);
if (jsonp) {
jsonSupport.writeJsonpValue(out, packet.getData());
} else {
jsonSupport.writeValue(out, packet.getData());
}
break;
}
case MESSAGE: {
byte subType = toChar(packet.getSubType().getValue());
buf.writeByte(subType);
if (packet.getSubType() == PacketType.CONNECT) {
if (!packet.getNsp().isEmpty()) {
buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
}
} else {
if (!packet.getNsp().isEmpty()) {
buf.writeBytes(packet.getNsp().getBytes(CharsetUtil.UTF_8));
buf.writeBytes(new byte[] {','});
}
}
if (packet.getAckId() != null) {
byte[] ackId = toChars(packet.getAckId());
buf.writeBytes(ackId);
}
List<Object> values = new ArrayList<Object>();
if (packet.getSubType() == PacketType.EVENT
|| packet.getSubType() == PacketType.ERROR) {
values.add(packet.getName());
}
if (packet.getSubType() == PacketType.EVENT
|| packet.getSubType() == PacketType.ACK
|| packet.getSubType() == PacketType.ERROR) {
List<Object> args = packet.getData();
values.addAll(args);
ByteBufOutputStream out = new ByteBufOutputStream(buf);
if (jsonp) {
jsonSupport.writeJsonpValue(out, values);
} else {
jsonSupport.writeValue(out, values);
}