compatible with socket.io-parser 2.2.0, and supports protocol v4
This commit is contained in:
@@ -17,7 +17,7 @@ public class Binary {
|
||||
public static DeconstructedPacket deconstructPacket(Packet packet) {
|
||||
List<byte[]> buffers = new ArrayList<byte[]>();
|
||||
|
||||
packet.data = deconstructBinPackRecursive(packet.data, buffers);
|
||||
packet.data = _deconstructPacket(packet.data, buffers);
|
||||
packet.attachments = buffers.size();
|
||||
|
||||
DeconstructedPacket result = new DeconstructedPacket();
|
||||
@@ -26,7 +26,7 @@ public class Binary {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Object deconstructBinPackRecursive(Object data, List<byte[]> buffers) {
|
||||
private static Object _deconstructPacket(Object data, List<byte[]> buffers) {
|
||||
if (data == null) return null;
|
||||
|
||||
if (data instanceof byte[]) {
|
||||
@@ -40,7 +40,7 @@ public class Binary {
|
||||
JSONArray _data = (JSONArray)data;
|
||||
int len = _data.length();
|
||||
for (int i = 0; i < len; i ++) {
|
||||
newData.put(i, deconstructBinPackRecursive(_data.get(i), buffers));
|
||||
newData.put(i, _deconstructPacket(_data.get(i), buffers));
|
||||
}
|
||||
return newData;
|
||||
} else if (data instanceof JSONObject) {
|
||||
@@ -49,7 +49,7 @@ public class Binary {
|
||||
Iterator<?> iterator = _data.keys();
|
||||
while (iterator.hasNext()) {
|
||||
String key = (String)iterator.next();
|
||||
newData.put(key, deconstructBinPackRecursive(_data.get(key), buffers));
|
||||
newData.put(key, _deconstructPacket(_data.get(key), buffers));
|
||||
}
|
||||
return newData;
|
||||
}
|
||||
@@ -57,17 +57,17 @@ public class Binary {
|
||||
}
|
||||
|
||||
public static Packet reconstructPacket(Packet packet, byte[][] buffers) {
|
||||
packet.data = reconstructBinPackRecursive(packet.data, buffers);
|
||||
packet.data = _reconstructPacket(packet.data, buffers);
|
||||
packet.attachments = -1;
|
||||
return packet;
|
||||
}
|
||||
|
||||
private static Object reconstructBinPackRecursive(Object data, byte[][] buffers) {
|
||||
private static Object _reconstructPacket(Object data, byte[][] buffers) {
|
||||
if (data instanceof JSONArray) {
|
||||
JSONArray _data = (JSONArray)data;
|
||||
int len = _data.length();
|
||||
for (int i = 0; i < len; i ++) {
|
||||
_data.put(i, reconstructBinPackRecursive(_data.get(i), buffers));
|
||||
_data.put(i, _reconstructPacket(_data.get(i), buffers));
|
||||
}
|
||||
return _data;
|
||||
} else if (data instanceof JSONObject) {
|
||||
@@ -79,7 +79,7 @@ public class Binary {
|
||||
Iterator<?> iterator = _data.keys();
|
||||
while (iterator.hasNext()) {
|
||||
String key = (String)iterator.next();
|
||||
_data.put(key, reconstructBinPackRecursive(_data.get(key), buffers));
|
||||
_data.put(key, _reconstructPacket(_data.get(key), buffers));
|
||||
}
|
||||
return _data;
|
||||
}
|
||||
|
||||
@@ -43,7 +43,12 @@ public class Parser {
|
||||
*/
|
||||
public static final int BINARY_EVENT = 5;
|
||||
|
||||
public static int protocol = 3;
|
||||
/**
|
||||
* Packet type `binary ack`.
|
||||
*/
|
||||
public static final int BINARY_ACK = 6;
|
||||
|
||||
public static int protocol = 4;
|
||||
|
||||
/**
|
||||
* Packet types.
|
||||
@@ -54,6 +59,7 @@ public class Parser {
|
||||
"EVENT",
|
||||
"BINARY_EVENT",
|
||||
"ACK",
|
||||
"BINARY_ACK",
|
||||
"ERROR",
|
||||
};
|
||||
|
||||
@@ -72,7 +78,7 @@ public class Parser {
|
||||
public void encode(Packet obj, Callback callback) {
|
||||
logger.fine(String.format("encoding packet %s", obj));
|
||||
|
||||
if (BINARY_EVENT == obj.type || ACK == obj.type) {
|
||||
if (BINARY_EVENT == obj.type || BINARY_ACK == obj.type) {
|
||||
encodeAsBinary(obj, callback);
|
||||
} else {
|
||||
String encoding = encodeAsString(obj);
|
||||
@@ -86,7 +92,7 @@ public class Parser {
|
||||
|
||||
str.append(obj.type);
|
||||
|
||||
if (BINARY_EVENT == obj.type || ACK == obj.type) {
|
||||
if (BINARY_EVENT == obj.type || BINARY_ACK == obj.type) {
|
||||
str.append(obj.attachments);
|
||||
str.append("-");
|
||||
}
|
||||
@@ -140,7 +146,7 @@ public class Parser {
|
||||
|
||||
public void add(String obj) {
|
||||
Packet packet = decodeString(obj);
|
||||
if (BINARY_EVENT == packet.type || ACK == packet.type) {
|
||||
if (BINARY_EVENT == packet.type || BINARY_ACK == packet.type) {
|
||||
this.reconstructor = new BinaryReconstructor(packet);
|
||||
|
||||
if (this.reconstructor.reconPack.attachments == 0) {
|
||||
@@ -170,7 +176,7 @@ public class Parser {
|
||||
p.type = Character.getNumericValue(str.charAt(0));
|
||||
if (p.type < 0 || p.type > types.length - 1) return error();
|
||||
|
||||
if (BINARY_EVENT == p.type || ACK == p.type) {
|
||||
if (BINARY_EVENT == p.type || BINARY_ACK == p.type) {
|
||||
StringBuilder attachments = new StringBuilder();
|
||||
while (str.charAt(++i) != '-') {
|
||||
attachments.append(str.charAt(i));
|
||||
|
||||
Reference in New Issue
Block a user