make Packet a generic class
This commit is contained in:
@@ -149,7 +149,7 @@ public class Socket extends Emitter {
|
|||||||
JSONArray jsonArgs = new JSONArray(_args);
|
JSONArray jsonArgs = new JSONArray(_args);
|
||||||
int parserType = Parser.EVENT;
|
int parserType = Parser.EVENT;
|
||||||
if (HasBinaryData.hasBinary(jsonArgs)) { parserType = Parser.BINARY_EVENT; }
|
if (HasBinaryData.hasBinary(jsonArgs)) { parserType = Parser.BINARY_EVENT; }
|
||||||
Packet packet = new Packet(parserType, jsonArgs);
|
Packet<JSONArray> packet = new Packet<JSONArray>(parserType, jsonArgs);
|
||||||
|
|
||||||
if (_args.get(_args.size() - 1) instanceof Ack) {
|
if (_args.get(_args.size() - 1) instanceof Ack) {
|
||||||
logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids));
|
logger.fine(String.format("emitting packet with ack id %d", Socket.this.ids));
|
||||||
@@ -181,7 +181,7 @@ public class Socket extends Emitter {
|
|||||||
addAll(Arrays.asList(args));
|
addAll(Arrays.asList(args));
|
||||||
}
|
}
|
||||||
}};
|
}};
|
||||||
Packet packet = new Packet(Parser.EVENT, new JSONArray(_args));
|
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.EVENT, new JSONArray(_args));
|
||||||
|
|
||||||
logger.fine(String.format("emitting packet with ack id %d", ids));
|
logger.fine(String.format("emitting packet with ack id %d", ids));
|
||||||
Socket.this.acks.put(ids, ack);
|
Socket.this.acks.put(ids, ack);
|
||||||
@@ -247,8 +247,8 @@ public class Socket extends Emitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onevent(Packet packet) {
|
private void onevent(Packet<JSONArray> packet) {
|
||||||
List<Object> args = new ArrayList<Object>(Arrays.asList(toArray((JSONArray) packet.data)));
|
List<Object> args = new ArrayList<Object>(Arrays.asList(toArray(packet.data)));
|
||||||
logger.fine(String.format("emitting event %s", args));
|
logger.fine(String.format("emitting event %s", args));
|
||||||
|
|
||||||
if (packet.id >= 0) {
|
if (packet.id >= 0) {
|
||||||
@@ -273,17 +273,17 @@ public class Socket extends Emitter {
|
|||||||
if (sent[0]) return;
|
if (sent[0]) return;
|
||||||
sent[0] = true;
|
sent[0] = true;
|
||||||
logger.fine(String.format("sending ack %s", args));
|
logger.fine(String.format("sending ack %s", args));
|
||||||
Packet packet = new Packet(Parser.ACK, new JSONArray(args));
|
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.ACK, new JSONArray(args));
|
||||||
packet.id = id;
|
packet.id = id;
|
||||||
self.packet(packet);
|
self.packet(packet);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onack(Packet packet) {
|
private void onack(Packet<JSONArray> packet) {
|
||||||
logger.fine(String.format("calling ack %s with %s", packet.id, packet.data));
|
logger.fine(String.format("calling ack %s with %s", packet.id, packet.data));
|
||||||
Ack fn = this.acks.remove(packet.id);
|
Ack fn = this.acks.remove(packet.id);
|
||||||
fn.call(toArray((JSONArray) packet.data));
|
fn.call(toArray(packet.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onconnect() {
|
private void onconnect() {
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
package com.github.nkzawa.socketio.parser;
|
package com.github.nkzawa.socketio.parser;
|
||||||
|
|
||||||
|
|
||||||
public class Packet {
|
public class Packet<T> {
|
||||||
|
|
||||||
public int type = -1;
|
public int type = -1;
|
||||||
public int id = -1;
|
public int id = -1;
|
||||||
public String nsp;
|
public String nsp;
|
||||||
public Object data;
|
public T data;
|
||||||
public int attachments;
|
public int attachments;
|
||||||
|
|
||||||
public Packet() {}
|
public Packet() {}
|
||||||
@@ -15,7 +15,7 @@ public class Packet {
|
|||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Packet(int type, Object data) {
|
public Packet(int type, T data) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,8 +60,8 @@ public class Parser {
|
|||||||
|
|
||||||
private Parser() {}
|
private Parser() {}
|
||||||
|
|
||||||
private static Packet error() {
|
private static Packet<String> error() {
|
||||||
return new Packet(ERROR, "parser error");
|
return new Packet<String>(ERROR, "parser error");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package com.github.nkzawa.socketio.parser;
|
|||||||
import com.github.nkzawa.emitter.Emitter;
|
import com.github.nkzawa.emitter.Emitter;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONTokener;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
@@ -36,21 +35,21 @@ public class ParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void encodeEvent() {
|
public void encodeEvent() {
|
||||||
Packet packet1 = new Packet(Parser.EVENT);
|
Packet<JSONArray> packet1 = new Packet<JSONArray>(Parser.EVENT);
|
||||||
packet1.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
packet1.data = new JSONArray("[\"a\", 1, {}]");
|
||||||
packet1.nsp = "/";
|
packet1.nsp = "/";
|
||||||
test(packet1);
|
test(packet1);
|
||||||
|
|
||||||
Packet packet2 = new Packet(Parser.EVENT);
|
Packet<JSONArray> packet2 = new Packet<JSONArray>(Parser.EVENT);
|
||||||
packet2.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
packet2.data = new JSONArray("[\"a\", 1, {}]");
|
||||||
packet2.nsp = "/test";
|
packet2.nsp = "/test";
|
||||||
test(packet2);
|
test(packet2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void encodeAck() {
|
public void encodeAck() {
|
||||||
Packet packet = new Packet(Parser.ACK);
|
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.ACK);
|
||||||
packet.data = new JSONTokener("[\"a\", 1, {}]").nextValue();
|
packet.data = new JSONArray("[\"a\", 1, {}]");
|
||||||
packet.id = 123;
|
packet.id = 123;
|
||||||
packet.nsp = "/";
|
packet.nsp = "/";
|
||||||
test(packet);
|
test(packet);
|
||||||
@@ -58,7 +57,7 @@ public class ParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void encodeByteArray() {
|
public void encodeByteArray() {
|
||||||
Packet packet = new Packet(Parser.BINARY_EVENT);
|
Packet<byte[]> packet = new Packet<byte[]>(Parser.BINARY_EVENT);
|
||||||
packet.data = "abc".getBytes(Charset.forName("UTF-8"));
|
packet.data = "abc".getBytes(Charset.forName("UTF-8"));
|
||||||
packet.id = 23;
|
packet.id = 23;
|
||||||
packet.nsp = "/cool";
|
packet.nsp = "/cool";
|
||||||
@@ -67,7 +66,7 @@ public class ParserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void encodeByteArray2() {
|
public void encodeByteArray2() {
|
||||||
Packet packet = new Packet(Parser.BINARY_EVENT);
|
Packet<byte[]> packet = new Packet<byte[]>(Parser.BINARY_EVENT);
|
||||||
packet.data = new byte[2];
|
packet.data = new byte[2];
|
||||||
packet.id = 0;
|
packet.id = 0;
|
||||||
packet.nsp = "/";
|
packet.nsp = "/";
|
||||||
@@ -80,7 +79,7 @@ public class ParserTest {
|
|||||||
data.getJSONObject("b").put("why", new byte[3]);
|
data.getJSONObject("b").put("why", new byte[3]);
|
||||||
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
|
data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]);
|
||||||
|
|
||||||
Packet packet = new Packet(Parser.BINARY_EVENT);
|
Packet<JSONObject> packet = new Packet<JSONObject>(Parser.BINARY_EVENT);
|
||||||
packet.data = data;
|
packet.data = data;
|
||||||
packet.id = 999;
|
packet.id = 999;
|
||||||
packet.nsp = "/deep";
|
packet.nsp = "/deep";
|
||||||
@@ -93,7 +92,7 @@ public class ParserTest {
|
|||||||
data.put(new byte[2]);
|
data.put(new byte[2]);
|
||||||
data.put(new byte[3]);
|
data.put(new byte[3]);
|
||||||
|
|
||||||
Packet packet = new Packet(Parser.BINARY_EVENT);
|
Packet<JSONArray> packet = new Packet<JSONArray>(Parser.BINARY_EVENT);
|
||||||
packet.data = data;
|
packet.data = data;
|
||||||
packet.id = 0;
|
packet.id = 0;
|
||||||
packet.nsp = "/";
|
packet.nsp = "/";
|
||||||
|
|||||||
Reference in New Issue
Block a user