diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java index 357233d..7fa309a 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java +++ b/src/main/java/com/corundumstudio/socketio/protocol/PacketDecoder.java @@ -56,9 +56,10 @@ public class PacketDecoder { startPos = 2; } + int slashesCount = packet.split("\\\\\\\\\\\\n").length - 1; int splitIndex = packet.indexOf(":"); String len = packet.substring(startPos, splitIndex); - Integer length = Integer.valueOf(len); + Integer length = Integer.valueOf(len) + slashesCount; packet = packet.substring(splitIndex+1, splitIndex+length+1); packet = new String(packet.getBytes(CharsetUtil.ISO_8859_1), CharsetUtil.UTF_8); diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java index a94b535..618b3cc 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java +++ b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java @@ -74,6 +74,10 @@ public class PacketEncoder { encodePacket(packet, packetBuf, allocator, true); int packetSize = packetBuf.writerIndex(); + if (jsonpMode) { + int count = count(packetBuf, Unpooled.copiedBuffer("\\\\\\n", CharsetUtil.UTF_8)); + packetSize -= count; + } buf.writeBytes(toChars(packetSize)); buf.writeBytes(B64_DELIMITER); @@ -100,7 +104,7 @@ public class PacketEncoder { String packet = buf.toString(CharsetUtil.UTF_8); buf.release(); // TODO optimize - packet = packet.replace("\\", "\\\\").replace("'", "\\'"); + packet = packet.replace("\\", "\\\\").replace("'", "\\'").replace("\\\\\\\\\\n", "\\\\\\n"); packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1); out.writeBytes(packet.getBytes(CharsetUtil.UTF_8)); @@ -315,6 +319,16 @@ public class PacketEncoder { } } + private int count(ByteBuf buffer, ByteBuf searchValue) { + int count = 0; + for (int i = 0; i < buffer.readableBytes(); i++) { + if (isValueFound(buffer, i, searchValue)) { + count++; + } + } + return count; + } + public static int find(ByteBuf buffer, ByteBuf searchValue) { for (int i = buffer.readerIndex(); i < buffer.readerIndex() + buffer.readableBytes(); i++) { if (isValueFound(buffer, i, searchValue)) {