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:
@@ -2,6 +2,7 @@ package io.socket.client;
|
||||
|
||||
import io.socket.backo.Backoff;
|
||||
import io.socket.emitter.Emitter;
|
||||
import io.socket.parser.DecodingException;
|
||||
import io.socket.parser.IOParser;
|
||||
import io.socket.parser.Packet;
|
||||
import io.socket.parser.Parser;
|
||||
@@ -10,16 +11,7 @@ import okhttp3.Call;
|
||||
import okhttp3.WebSocket;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.Set;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
@@ -48,16 +40,6 @@ public class Manager extends Emitter {
|
||||
public static final String EVENT_PACKET = "packet";
|
||||
public static final String EVENT_ERROR = "error";
|
||||
|
||||
/**
|
||||
* Called on a connection error.
|
||||
*/
|
||||
public static final String EVENT_CONNECT_ERROR = "connect_error";
|
||||
|
||||
/**
|
||||
* Called on a connection timeout.
|
||||
*/
|
||||
public static final String EVENT_CONNECT_TIMEOUT = "connect_timeout";
|
||||
|
||||
/**
|
||||
* Called on a successful reconnection.
|
||||
*/
|
||||
@@ -72,12 +54,6 @@ public class Manager extends Emitter {
|
||||
|
||||
public static final String EVENT_RECONNECT_ATTEMPT = "reconnect_attempt";
|
||||
|
||||
public static final String EVENT_RECONNECTING = "reconnecting";
|
||||
|
||||
public static final String EVENT_PING = "ping";
|
||||
|
||||
public static final String EVENT_PONG = "pong";
|
||||
|
||||
/**
|
||||
* Called when a new transport is created. (experimental)
|
||||
*/
|
||||
@@ -98,8 +74,6 @@ public class Manager extends Emitter {
|
||||
private double _randomizationFactor;
|
||||
private Backoff backoff;
|
||||
private long _timeout;
|
||||
private Set<Socket> connecting = new HashSet<Socket>();
|
||||
private Date lastPing;
|
||||
private URI uri;
|
||||
private List<Packet> packetBuffer;
|
||||
private Queue<On.Handle> subs;
|
||||
@@ -160,28 +134,6 @@ public class Manager extends Emitter {
|
||||
this.decoder = opts.decoder != null ? opts.decoder : new IOParser.Decoder();
|
||||
}
|
||||
|
||||
private void emitAll(String event, Object... args) {
|
||||
this.emit(event, args);
|
||||
for (Socket socket : this.nsps.values()) {
|
||||
socket.emit(event, args);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update `socket.id` of all sockets
|
||||
*/
|
||||
private void updateSocketIds() {
|
||||
for (Map.Entry<String, Socket> entry : this.nsps.entrySet()) {
|
||||
String nsp = entry.getKey();
|
||||
Socket socket = entry.getValue();
|
||||
socket.id = this.generateId(nsp);
|
||||
}
|
||||
}
|
||||
|
||||
private String generateId(String nsp) {
|
||||
return ("/".equals(nsp) ? "" : (nsp + "#")) + this.engine.id();
|
||||
}
|
||||
|
||||
public boolean reconnection() {
|
||||
return this._reconnection;
|
||||
}
|
||||
@@ -307,7 +259,7 @@ public class Manager extends Emitter {
|
||||
logger.fine("connect_error");
|
||||
self.cleanup();
|
||||
self.readyState = ReadyState.CLOSED;
|
||||
self.emitAll(EVENT_CONNECT_ERROR, data);
|
||||
self.emit(EVENT_ERROR, data);
|
||||
if (fn != null) {
|
||||
Exception err = new SocketIOException("Connection error",
|
||||
data instanceof Exception ? (Exception) data : null);
|
||||
@@ -334,7 +286,6 @@ public class Manager extends Emitter {
|
||||
openSub.destroy();
|
||||
socket.close();
|
||||
socket.emit(Engine.EVENT_ERROR, new SocketIOException("timeout"));
|
||||
self.emitAll(EVENT_CONNECT_TIMEOUT, timeout);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -377,18 +328,6 @@ public class Manager extends Emitter {
|
||||
}
|
||||
}
|
||||
}));
|
||||
this.subs.add(On.on(socket, Engine.EVENT_PING, new Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
Manager.this.onping();
|
||||
}
|
||||
}));
|
||||
this.subs.add(On.on(socket, Engine.EVENT_PONG, new Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
Manager.this.onpong();
|
||||
}
|
||||
}));
|
||||
this.subs.add(On.on(socket, Engine.EVENT_ERROR, new Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
@@ -409,22 +348,20 @@ public class Manager extends Emitter {
|
||||
});
|
||||
}
|
||||
|
||||
private void onping() {
|
||||
this.lastPing = new Date();
|
||||
this.emitAll(EVENT_PING);
|
||||
}
|
||||
|
||||
private void onpong() {
|
||||
this.emitAll(EVENT_PONG,
|
||||
null != this.lastPing ? new Date().getTime() - this.lastPing.getTime() : 0);
|
||||
}
|
||||
|
||||
private void ondata(String data) {
|
||||
this.decoder.add(data);
|
||||
try {
|
||||
this.decoder.add(data);
|
||||
} catch (DecodingException e) {
|
||||
this.onerror(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void ondata(byte[] data) {
|
||||
this.decoder.add(data);
|
||||
try {
|
||||
this.decoder.add(data);
|
||||
} catch (DecodingException e) {
|
||||
this.onerror(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void ondecoded(Packet packet) {
|
||||
@@ -433,7 +370,7 @@ public class Manager extends Emitter {
|
||||
|
||||
private void onerror(Exception err) {
|
||||
logger.log(Level.FINE, "error", err);
|
||||
this.emitAll(EVENT_ERROR, err);
|
||||
this.emit(EVENT_ERROR, err);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -444,41 +381,31 @@ public class Manager extends Emitter {
|
||||
* @return a socket instance for the namespace.
|
||||
*/
|
||||
public Socket socket(final String nsp, Options opts) {
|
||||
Socket socket = this.nsps.get(nsp);
|
||||
if (socket == null) {
|
||||
socket = new Socket(this, nsp, opts);
|
||||
Socket _socket = this.nsps.putIfAbsent(nsp, socket);
|
||||
if (_socket != null) {
|
||||
socket = _socket;
|
||||
} else {
|
||||
final Manager self = this;
|
||||
final Socket s = socket;
|
||||
socket.on(Socket.EVENT_CONNECTING, new Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
self.connecting.add(s);
|
||||
}
|
||||
});
|
||||
socket.on(Socket.EVENT_CONNECT, new Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
s.id = self.generateId(nsp);
|
||||
}
|
||||
});
|
||||
synchronized (this.nsps) {
|
||||
Socket socket = this.nsps.get(nsp);
|
||||
if (socket == null) {
|
||||
socket = new Socket(this, nsp, opts);
|
||||
this.nsps.put(nsp, socket);
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
return socket;
|
||||
}
|
||||
|
||||
public Socket socket(String nsp) {
|
||||
return socket(nsp, null);
|
||||
}
|
||||
|
||||
/*package*/ void destroy(Socket socket) {
|
||||
this.connecting.remove(socket);
|
||||
if (!this.connecting.isEmpty()) return;
|
||||
/*package*/ void destroy() {
|
||||
synchronized (this.nsps) {
|
||||
for (Socket socket : this.nsps.values()) {
|
||||
if (socket.isActive()) {
|
||||
logger.fine("socket is still active, skipping close");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
this.close();
|
||||
this.close();
|
||||
}
|
||||
}
|
||||
|
||||
/*package*/ void packet(Packet packet) {
|
||||
@@ -487,10 +414,6 @@ public class Manager extends Emitter {
|
||||
}
|
||||
final Manager self = this;
|
||||
|
||||
if (packet.query != null && !packet.query.isEmpty() && packet.type == Parser.CONNECT) {
|
||||
packet.nsp += "?" + packet.query;
|
||||
}
|
||||
|
||||
if (!self.encoding) {
|
||||
self.encoding = true;
|
||||
this.encoder.encode(packet, new Parser.Encoder.Callback() {
|
||||
@@ -528,7 +451,6 @@ public class Manager extends Emitter {
|
||||
|
||||
this.packetBuffer.clear();
|
||||
this.encoding = false;
|
||||
this.lastPing = null;
|
||||
|
||||
this.decoder.destroy();
|
||||
}
|
||||
@@ -569,7 +491,7 @@ public class Manager extends Emitter {
|
||||
if (this.backoff.getAttempts() >= this._reconnectionAttempts) {
|
||||
logger.fine("reconnect failed");
|
||||
this.backoff.reset();
|
||||
this.emitAll(EVENT_RECONNECT_FAILED);
|
||||
this.emit(EVENT_RECONNECT_FAILED);
|
||||
this.reconnecting = false;
|
||||
} else {
|
||||
long delay = this.backoff.duration();
|
||||
@@ -587,8 +509,7 @@ public class Manager extends Emitter {
|
||||
|
||||
logger.fine("attempting reconnect");
|
||||
int attempts = self.backoff.getAttempts();
|
||||
self.emitAll(EVENT_RECONNECT_ATTEMPT, attempts);
|
||||
self.emitAll(EVENT_RECONNECTING, attempts);
|
||||
self.emit(EVENT_RECONNECT_ATTEMPT, attempts);
|
||||
|
||||
// check again for the case socket closed in above events
|
||||
if (self.skipReconnect) return;
|
||||
@@ -600,7 +521,7 @@ public class Manager extends Emitter {
|
||||
logger.fine("reconnect attempt error");
|
||||
self.reconnecting = false;
|
||||
self.reconnect();
|
||||
self.emitAll(EVENT_RECONNECT_ERROR, err);
|
||||
self.emit(EVENT_RECONNECT_ERROR, err);
|
||||
} else {
|
||||
logger.fine("reconnect success");
|
||||
self.onreconnect();
|
||||
@@ -625,8 +546,7 @@ public class Manager extends Emitter {
|
||||
int attempts = this.backoff.getAttempts();
|
||||
this.reconnecting = false;
|
||||
this.backoff.reset();
|
||||
this.updateSocketIds();
|
||||
this.emitAll(EVENT_RECONNECT, attempts);
|
||||
this.emit(EVENT_RECONNECT, attempts);
|
||||
}
|
||||
|
||||
|
||||
@@ -652,6 +572,7 @@ public class Manager extends Emitter {
|
||||
public double randomizationFactor;
|
||||
public Parser.Encoder encoder;
|
||||
public Parser.Decoder decoder;
|
||||
public Map<String, String> auth;
|
||||
|
||||
/**
|
||||
* Connection timeout (ms). Set -1 to disable.
|
||||
|
||||
Reference in New Issue
Block a user