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