feat: add support for Socket.IO v3
Including: -969debe88c-6494f61be0-132f8ec918-f8f60fc860Reference: https://github.com/socketio/socket.io-protocol#difference-between-v5-and-v4
This commit is contained in:
@@ -8,13 +8,7 @@ import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -30,8 +24,6 @@ public class Socket extends Emitter {
|
||||
*/
|
||||
public static final String EVENT_CONNECT = "connect";
|
||||
|
||||
public static final String EVENT_CONNECTING = "connecting";
|
||||
|
||||
/**
|
||||
* Called on a disconnection.
|
||||
*/
|
||||
@@ -45,42 +37,18 @@ public class Socket extends Emitter {
|
||||
* <li>(Exception) error data.</li>
|
||||
* </ul>
|
||||
*/
|
||||
public static final String EVENT_ERROR = "error";
|
||||
public static final String EVENT_CONNECT_ERROR = "connect_error";
|
||||
|
||||
public static final String EVENT_MESSAGE = "message";
|
||||
static final String EVENT_MESSAGE = "message";
|
||||
|
||||
public static final String EVENT_CONNECT_ERROR = Manager.EVENT_CONNECT_ERROR;
|
||||
|
||||
public static final String EVENT_CONNECT_TIMEOUT = Manager.EVENT_CONNECT_TIMEOUT;
|
||||
|
||||
public static final String EVENT_RECONNECT = Manager.EVENT_RECONNECT;
|
||||
|
||||
public static final String EVENT_RECONNECT_ERROR = Manager.EVENT_RECONNECT_ERROR;
|
||||
|
||||
public static final String EVENT_RECONNECT_FAILED = Manager.EVENT_RECONNECT_FAILED;
|
||||
|
||||
public static final String EVENT_RECONNECT_ATTEMPT = Manager.EVENT_RECONNECT_ATTEMPT;
|
||||
|
||||
public static final String EVENT_RECONNECTING = Manager.EVENT_RECONNECTING;
|
||||
|
||||
public static final String EVENT_PING = Manager.EVENT_PING;
|
||||
|
||||
public static final String EVENT_PONG = Manager.EVENT_PONG;
|
||||
|
||||
protected static Map<String, Integer> events = new HashMap<String, Integer>() {{
|
||||
protected static Map<String, Integer> RESERVED_EVENTS = new HashMap<String, Integer>() {{
|
||||
put(EVENT_CONNECT, 1);
|
||||
put(EVENT_CONNECT_ERROR, 1);
|
||||
put(EVENT_CONNECT_TIMEOUT, 1);
|
||||
put(EVENT_CONNECTING, 1);
|
||||
put(EVENT_DISCONNECT, 1);
|
||||
put(EVENT_ERROR, 1);
|
||||
put(EVENT_RECONNECT, 1);
|
||||
put(EVENT_RECONNECT_ATTEMPT, 1);
|
||||
put(EVENT_RECONNECT_FAILED, 1);
|
||||
put(EVENT_RECONNECT_ERROR, 1);
|
||||
put(EVENT_RECONNECTING, 1);
|
||||
put(EVENT_PING, 1);
|
||||
put(EVENT_PONG, 1);
|
||||
// used on the server-side
|
||||
put("disconnecting", 1);
|
||||
put("newListener", 1);
|
||||
put("removeListener", 1);
|
||||
}};
|
||||
|
||||
/*package*/ String id;
|
||||
@@ -89,7 +57,7 @@ public class Socket extends Emitter {
|
||||
private int ids;
|
||||
private String nsp;
|
||||
private Manager io;
|
||||
private String query;
|
||||
private Map<String, String> auth;
|
||||
private Map<Integer, Ack> acks = new HashMap<Integer, Ack>();
|
||||
private Queue<On.Handle> subs;
|
||||
private final Queue<List<Object>> receiveBuffer = new LinkedList<List<Object>>();
|
||||
@@ -99,7 +67,7 @@ public class Socket extends Emitter {
|
||||
this.io = io;
|
||||
this.nsp = nsp;
|
||||
if (opts != null) {
|
||||
this.query = opts.query;
|
||||
this.auth = opts.auth;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,6 +88,12 @@ public class Socket extends Emitter {
|
||||
Socket.this.onpacket((Packet<?>) args[0]);
|
||||
}
|
||||
}));
|
||||
add(On.on(io, Manager.EVENT_ERROR, new Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
Socket.super.emit(EVENT_CONNECT_ERROR, args[0]);
|
||||
}
|
||||
}));
|
||||
add(On.on(io, Manager.EVENT_CLOSE, new Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
@@ -129,6 +103,10 @@ public class Socket extends Emitter {
|
||||
}};
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return this.subs != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects the socket.
|
||||
*/
|
||||
@@ -141,7 +119,6 @@ public class Socket extends Emitter {
|
||||
Socket.this.subEvents();
|
||||
Socket.this.io.open(); // ensure open
|
||||
if (Manager.ReadyState.OPEN == Socket.this.io.readyState) Socket.this.onopen();
|
||||
Socket.this.emit(EVENT_CONNECTING);
|
||||
}
|
||||
});
|
||||
return this;
|
||||
@@ -179,14 +156,13 @@ public class Socket extends Emitter {
|
||||
*/
|
||||
@Override
|
||||
public Emitter emit(final String event, final Object... args) {
|
||||
if (RESERVED_EVENTS.containsKey(event)) {
|
||||
throw new RuntimeException("'" + event + "' is a reserved event name");
|
||||
}
|
||||
|
||||
EventThread.exec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (events.containsKey(event)) {
|
||||
Socket.super.emit(event, args);
|
||||
return;
|
||||
}
|
||||
|
||||
Ack ack;
|
||||
Object[] _args;
|
||||
int lastIndex = args.length - 1;
|
||||
@@ -255,14 +231,10 @@ public class Socket extends Emitter {
|
||||
private void onopen() {
|
||||
logger.fine("transport is open - connecting");
|
||||
|
||||
if (!"/".equals(this.nsp)) {
|
||||
if (this.query != null && !this.query.isEmpty()) {
|
||||
Packet packet = new Packet(Parser.CONNECT);
|
||||
packet.query = this.query;
|
||||
this.packet(packet);
|
||||
} else {
|
||||
this.packet(new Packet(Parser.CONNECT));
|
||||
}
|
||||
if (this.auth != null) {
|
||||
this.packet(new Packet<>(Parser.CONNECT, new JSONObject(this.auth)));
|
||||
} else {
|
||||
this.packet(new Packet<>(Parser.CONNECT));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,16 +244,24 @@ public class Socket extends Emitter {
|
||||
}
|
||||
this.connected = false;
|
||||
this.id = null;
|
||||
this.emit(EVENT_DISCONNECT, reason);
|
||||
super.emit(EVENT_DISCONNECT, reason);
|
||||
}
|
||||
|
||||
private void onpacket(Packet<?> packet) {
|
||||
if (!this.nsp.equals(packet.nsp)) return;
|
||||
|
||||
switch (packet.type) {
|
||||
case Parser.CONNECT:
|
||||
this.onconnect();
|
||||
case Parser.CONNECT: {
|
||||
if (packet.data instanceof JSONObject && ((JSONObject) packet.data).has("sid")) {
|
||||
try {
|
||||
this.onconnect(((JSONObject) packet.data).getString("sid"));
|
||||
return;
|
||||
} catch (JSONException e) {}
|
||||
} else {
|
||||
super.emit(EVENT_CONNECT_ERROR, new SocketIOException("It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, which is not possible"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case Parser.EVENT: {
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -315,8 +295,8 @@ public class Socket extends Emitter {
|
||||
this.ondisconnect();
|
||||
break;
|
||||
|
||||
case Parser.ERROR:
|
||||
this.emit(EVENT_ERROR, packet.data);
|
||||
case Parser.CONNECT_ERROR:
|
||||
super.emit(EVENT_CONNECT_ERROR, packet.data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -384,9 +364,10 @@ public class Socket extends Emitter {
|
||||
}
|
||||
}
|
||||
|
||||
private void onconnect() {
|
||||
private void onconnect(String id) {
|
||||
this.connected = true;
|
||||
this.emit(EVENT_CONNECT);
|
||||
this.id = id;
|
||||
super.emit(EVENT_CONNECT);
|
||||
this.emitBuffered();
|
||||
}
|
||||
|
||||
@@ -422,7 +403,7 @@ public class Socket extends Emitter {
|
||||
this.subs = null;
|
||||
}
|
||||
|
||||
this.io.destroy(this);
|
||||
this.io.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user