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:
4
pom.xml
4
pom.xml
@@ -2,7 +2,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>io.socket</groupId>
|
||||
<artifactId>socket.io-client</artifactId>
|
||||
<version>1.0.2-SNAPSHOT</version>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>socket.io-client</name>
|
||||
<description>Socket.IO Client Library for Java</description>
|
||||
@@ -62,7 +62,7 @@
|
||||
<dependency>
|
||||
<groupId>io.socket</groupId>
|
||||
<artifactId>engine.io-client</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
|
||||
@@ -72,6 +72,11 @@ public class IO {
|
||||
boolean newConnection = opts.forceNew || !opts.multiplex || sameNamespace;
|
||||
Manager io;
|
||||
|
||||
String query = parsed.getQuery();
|
||||
if (query != null && (opts.query == null || opts.query.isEmpty())) {
|
||||
opts.query = query;
|
||||
}
|
||||
|
||||
if (newConnection) {
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
logger.fine(String.format("ignoring socket cache for %s", source));
|
||||
@@ -87,11 +92,6 @@ public class IO {
|
||||
io = managers.get(id);
|
||||
}
|
||||
|
||||
String query = parsed.getQuery();
|
||||
if (query != null && (opts.query == null || opts.query.isEmpty())) {
|
||||
opts.query = query;
|
||||
}
|
||||
|
||||
return io.socket(parsed.getPath(), opts);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
try {
|
||||
this.decoder.add(data);
|
||||
} catch (DecodingException e) {
|
||||
this.onerror(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void ondata(byte[] 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,42 +381,32 @@ public class Manager extends Emitter {
|
||||
* @return a socket instance for the namespace.
|
||||
*/
|
||||
public Socket socket(final String nsp, Options opts) {
|
||||
synchronized (this.nsps) {
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
this.nsps.put(nsp, 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();
|
||||
}
|
||||
}
|
||||
|
||||
/*package*/ void packet(Packet packet) {
|
||||
if (logger.isLoggable(Level.FINE)) {
|
||||
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
if (this.auth != null) {
|
||||
this.packet(new Packet<>(Parser.CONNECT, new JSONObject(this.auth)));
|
||||
} else {
|
||||
this.packet(new Packet(Parser.CONNECT));
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
7
src/main/java/io/socket/parser/DecodingException.java
Normal file
7
src/main/java/io/socket/parser/DecodingException.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package io.socket.parser;
|
||||
|
||||
public class DecodingException extends RuntimeException {
|
||||
public DecodingException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,6 @@ final public class IOParser implements Parser {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(IOParser.class.getName());
|
||||
|
||||
private static Packet<String> error() {
|
||||
return new Packet<String>(ERROR, "parser error");
|
||||
}
|
||||
|
||||
private IOParser() {}
|
||||
|
||||
final public static class Encoder implements Parser.Encoder {
|
||||
@@ -128,10 +124,14 @@ final public class IOParser implements Parser {
|
||||
|
||||
Packet<Object> p = new Packet<Object>(Character.getNumericValue(str.charAt(0)));
|
||||
|
||||
if (p.type < 0 || p.type > types.length - 1) return error();
|
||||
if (p.type < 0 || p.type > types.length - 1) {
|
||||
throw new DecodingException("unknown packet type " + p.type);
|
||||
}
|
||||
|
||||
if (BINARY_EVENT == p.type || BINARY_ACK == p.type) {
|
||||
if (!str.contains("-") || length <= i + 1) return error();
|
||||
if (!str.contains("-") || length <= i + 1) {
|
||||
throw new DecodingException("illegal attachments");
|
||||
}
|
||||
StringBuilder attachments = new StringBuilder();
|
||||
while (str.charAt(++i) != '-') {
|
||||
attachments.append(str.charAt(i));
|
||||
@@ -170,7 +170,7 @@ final public class IOParser implements Parser {
|
||||
try {
|
||||
p.id = Integer.parseInt(id.toString());
|
||||
} catch (NumberFormatException e){
|
||||
return error();
|
||||
throw new DecodingException("invalid payload");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -181,7 +181,7 @@ final public class IOParser implements Parser {
|
||||
p.data = new JSONTokener(str.substring(i)).nextValue();
|
||||
} catch (JSONException e) {
|
||||
logger.log(Level.WARNING, "An error occured while retrieving data from JSONTokener", e);
|
||||
return error();
|
||||
throw new DecodingException("invalid payload");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,6 @@ public class Packet<T> {
|
||||
public String nsp;
|
||||
public T data;
|
||||
public int attachments;
|
||||
public String query;
|
||||
|
||||
public Packet() {}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public interface Parser {
|
||||
/**
|
||||
* Packet type `error`.
|
||||
*/
|
||||
public static final int ERROR = 4;
|
||||
public static final int CONNECT_ERROR = 4;
|
||||
|
||||
/**
|
||||
* Packet type `binary event`.
|
||||
@@ -37,7 +37,7 @@ public interface Parser {
|
||||
*/
|
||||
public static final int BINARY_ACK = 6;
|
||||
|
||||
public static int protocol = 4;
|
||||
public static int protocol = 5;
|
||||
|
||||
/**
|
||||
* Packet types.
|
||||
|
||||
@@ -357,7 +357,7 @@ public class ConnectionTest extends Connection {
|
||||
}).once(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
socket.on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
|
||||
socket.io().on(Manager.EVENT_RECONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
socket.disconnect();
|
||||
@@ -387,7 +387,7 @@ public class ConnectionTest extends Connection {
|
||||
opts.reconnectionDelay = 10;
|
||||
final Manager manager = new Manager(new URI(uri()), opts);
|
||||
socket = manager.socket("/timeout");
|
||||
socket.once(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
manager.once(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
final int[] reconnects = new int[] {0};
|
||||
@@ -431,13 +431,13 @@ public class ConnectionTest extends Connection {
|
||||
final long[] startTime = new long[] {0};
|
||||
final long[] prevDelay = new long[] {0};
|
||||
|
||||
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
manager.on(Manager.EVENT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
startTime[0] = new Date().getTime();
|
||||
}
|
||||
});
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
manager.on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
reconnects[0]++;
|
||||
@@ -449,7 +449,7 @@ public class ConnectionTest extends Connection {
|
||||
prevDelay[0] = delay;
|
||||
}
|
||||
});
|
||||
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(true);
|
||||
@@ -464,27 +464,6 @@ public class ConnectionTest extends Connection {
|
||||
manager.close();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void reconnectEventFireInSocket() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer("done");
|
||||
}
|
||||
});
|
||||
socket.open();
|
||||
new Timer().schedule(new TimerTask() {
|
||||
@Override
|
||||
public void run() {
|
||||
socket.io().engine.close();
|
||||
}
|
||||
}, 500);
|
||||
values.take();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void notReconnectWhenForceClosed() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
@@ -492,10 +471,10 @@ public class ConnectionTest extends Connection {
|
||||
opts.timeout = 0;
|
||||
opts.reconnectionDelay = 10;
|
||||
socket = IO.socket(uri() + "/invalid", opts);
|
||||
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
socket.io().on(Manager.EVENT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(false);
|
||||
@@ -521,10 +500,10 @@ public class ConnectionTest extends Connection {
|
||||
opts.timeout = 0;
|
||||
opts.reconnectionDelay = 10;
|
||||
socket = IO.socket(uri() + "/invalid", opts);
|
||||
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
socket.io().once(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(false);
|
||||
@@ -552,10 +531,10 @@ public class ConnectionTest extends Connection {
|
||||
opts.timeout = 0;
|
||||
opts.reconnectionDelay = 10;
|
||||
socket = client("/invalid", opts);
|
||||
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
socket.io().once(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
socket.once(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
socket.io().once(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer("done");
|
||||
@@ -722,7 +701,7 @@ public class ConnectionTest extends Connection {
|
||||
}
|
||||
};
|
||||
manager.on(Manager.EVENT_RECONNECT_ATTEMPT, cb);
|
||||
manager.on(Manager.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
manager.on(Manager.EVENT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
Timer timer = new Timer();
|
||||
@@ -763,8 +742,8 @@ public class ConnectionTest extends Connection {
|
||||
}
|
||||
};
|
||||
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, reconnectCb);
|
||||
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
manager.on(Manager.EVENT_RECONNECT_ATTEMPT, reconnectCb);
|
||||
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.close();
|
||||
@@ -798,8 +777,8 @@ public class ConnectionTest extends Connection {
|
||||
}
|
||||
};
|
||||
|
||||
socket.on(Socket.EVENT_RECONNECTING, reconnectCb);
|
||||
socket.on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
manager.on(Manager.EVENT_RECONNECT_ATTEMPT, reconnectCb);
|
||||
manager.on(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
socket.close();
|
||||
|
||||
@@ -14,10 +14,11 @@ import java.util.TimerTask;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
|
||||
import static java.util.Collections.singletonMap;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class SocketTest extends Connection {
|
||||
@@ -39,7 +40,7 @@ public class SocketTest extends Connection {
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id = values.take();
|
||||
assertThat(id.isPresent(), is(true));
|
||||
assertThat(id.get(), is(socket.io().engine.id()));
|
||||
assertThat(id.get(), not(socket.io().engine.id())); // distinct ID since Socket.IO v3
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@@ -58,7 +59,7 @@ public class SocketTest extends Connection {
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<String> id = values.take();
|
||||
assertThat(id.isPresent(), is(true));
|
||||
assertThat(id.get(), is("/foo#" + socket.io().engine.id()));
|
||||
assertThat(id.get(), is(not(socket.io().engine.id()))); // distinct ID since Socket.IO v3
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@@ -112,60 +113,23 @@ public class SocketTest extends Connection {
|
||||
if (err.isPresent()) throw err.get();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void pingAndPongWithLatency() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
final boolean[] pinged = new boolean[] { false };
|
||||
socket.once(Socket.EVENT_PING, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
pinged[0] = true;
|
||||
}
|
||||
});
|
||||
socket.once(Socket.EVENT_PONG, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
long ms = (long)args[0];
|
||||
values.offer(pinged[0]);
|
||||
values.offer(ms);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
boolean pinged = (boolean)values.take();
|
||||
assertThat(pinged, is(true));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
long ms = (long)values.take();
|
||||
assertThat(ms, greaterThanOrEqualTo(0L));
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldChangeSocketIdUponReconnection() throws URISyntaxException, InterruptedException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
socket = client();
|
||||
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
|
||||
socket.on(Socket.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
socket.io().on(Manager.EVENT_RECONNECT_ATTEMPT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
}
|
||||
});
|
||||
|
||||
socket.on(Socket.EVENT_RECONNECT, new Emitter.Listener() {
|
||||
socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... objects) {
|
||||
values.offer(Optional.ofNullable(socket.id()));
|
||||
@@ -233,4 +197,65 @@ public class SocketTest extends Connection {
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldAcceptAnAuthOption() throws URISyntaxException, InterruptedException, JSONException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
|
||||
IO.Options opts = new IO.Options();
|
||||
opts.auth = singletonMap("token", "abcd");
|
||||
socket = client("/abc", opts);
|
||||
socket.on("handshake", new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
JSONObject handshake = (JSONObject)args[0];
|
||||
values.offer(Optional.ofNullable(handshake));
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Optional<JSONObject> handshake = values.take();
|
||||
JSONObject query = handshake.get().getJSONObject("auth");
|
||||
assertThat(query.getString("token"), is("abcd"));
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldFireAnErrorEventOnMiddlewareFailure() throws URISyntaxException, InterruptedException, JSONException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
|
||||
socket = client("/no");
|
||||
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
values.offer(Optional.ofNullable(args[0]));
|
||||
}
|
||||
});
|
||||
socket.connect();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
JSONObject error = ((Optional<JSONObject>) values.take()).get();
|
||||
assertThat(error.getString("message"), is("auth failed"));
|
||||
assertThat(error.getJSONObject("data").getString("a"), is("b"));
|
||||
assertThat(error.getJSONObject("data").getInt("c"), is(3));
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
|
||||
@Test(timeout = TIMEOUT)
|
||||
public void shouldThrowOnReservedEvent() throws URISyntaxException, InterruptedException, JSONException {
|
||||
final BlockingQueue<Optional> values = new LinkedBlockingQueue<Optional>();
|
||||
|
||||
socket = client("/no");
|
||||
try {
|
||||
socket.emit("disconnecting", "goodbye");
|
||||
fail();
|
||||
} catch (RuntimeException e) {
|
||||
assertThat(e.getMessage(), is("'disconnecting' is a reserved event name"));
|
||||
}
|
||||
|
||||
socket.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,12 +21,7 @@ public class ConnectionFailure {
|
||||
options.callFactory = client;
|
||||
|
||||
final Socket socket = IO.socket("http://localhost:" + port, options);
|
||||
socket.on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("connect timeout");
|
||||
}
|
||||
}).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
|
||||
@Override
|
||||
public void call(Object... args) {
|
||||
System.out.println("connect error");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package io.socket.parser;
|
||||
|
||||
import io.socket.emitter.Emitter;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
@@ -10,12 +9,12 @@ import org.skyscreamer.jsonassert.JSONAssert;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
@RunWith(JUnit4.class)
|
||||
public class Helpers {
|
||||
|
||||
private static Parser.Encoder encoder = new IOParser.Encoder();
|
||||
private static Packet<String> errorPacket = new Packet<String>(Parser.ERROR, "parser error");
|
||||
|
||||
public static void test(final Packet obj) {
|
||||
encoder.encode(obj, new Parser.Encoder.Callback() {
|
||||
@@ -35,13 +34,10 @@ public class Helpers {
|
||||
|
||||
public static void testDecodeError(final String errorMessage) {
|
||||
Parser.Decoder decoder = new IOParser.Decoder();
|
||||
decoder.onDecoded(new IOParser.Decoder.Callback() {
|
||||
@Override
|
||||
public void call(Packet packet) {
|
||||
assertPacket(errorPacket, packet);
|
||||
}
|
||||
});
|
||||
try {
|
||||
decoder.add(errorMessage);
|
||||
fail();
|
||||
} catch (DecodingException e) {}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
326
src/test/resources/package-lock.json
generated
326
src/test/resources/package-lock.json
generated
@@ -2,6 +2,26 @@
|
||||
"requires": true,
|
||||
"lockfileVersion": 1,
|
||||
"dependencies": {
|
||||
"@types/component-emitter": {
|
||||
"version": "1.2.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/component-emitter/-/component-emitter-1.2.10.tgz",
|
||||
"integrity": "sha512-bsjleuRKWmGqajMerkzox19aGbscQX5rmmvvXl3wlIp5gMG1HgkiwPxsN5p070fBDKTNSPgojVbuY1+HWMbFhg=="
|
||||
},
|
||||
"@types/cookie": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/cookie/-/cookie-0.4.0.tgz",
|
||||
"integrity": "sha512-y7mImlc/rNkvCRmg8gC3/lj87S7pTUIJ6QGjwHR9WQJcFs+ZMTOaoPrkdFA/YdbuqVEmEbb5RdhVxMkAcgOnpg=="
|
||||
},
|
||||
"@types/cors": {
|
||||
"version": "2.8.9",
|
||||
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.9.tgz",
|
||||
"integrity": "sha512-zurD1ibz21BRlAOIKP8yhrxlqKx6L9VCwkB5kMiP6nZAhoF5MvC7qS1qPA7nRcr1GJolfkQC7/EAL4hdYejLtg=="
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "14.14.12",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.12.tgz",
|
||||
"integrity": "sha512-ASH8OPHMNlkdjrEdmoILmzFfsJICvhBsFfAum4aKZ/9U4B6M6tTmTPh+f3ttWdD74CEGV5XvXWkbyfSdXaTd7g=="
|
||||
},
|
||||
"accepts": {
|
||||
"version": "1.3.7",
|
||||
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz",
|
||||
@@ -11,26 +31,6 @@
|
||||
"negotiator": "0.6.2"
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"version": "0.8.2",
|
||||
"resolved": "https://registry.npmjs.org/after/-/after-0.8.2.tgz",
|
||||
"integrity": "sha1-/ts5T58OAqqXaOcCvaI7UF+ufh8="
|
||||
},
|
||||
"arraybuffer.slice": {
|
||||
"version": "0.0.7",
|
||||
"resolved": "https://registry.npmjs.org/arraybuffer.slice/-/arraybuffer.slice-0.0.7.tgz",
|
||||
"integrity": "sha512-wGUIVQXuehL5TCqQun8OW81jGzAWycqzFF8lFp+GOM5BXLYj3bKNsYC4daB7n6XjCqxQA/qgTJ+8ANR3acjrog=="
|
||||
},
|
||||
"async-limiter": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.1.tgz",
|
||||
"integrity": "sha512-csOlWGAcRFJaI6m+F2WKdnMKr4HhdhFVBk0H/QbJFMCr+uO2kwohwXQPxw/9OCxp05r5ghVBFSyioixx3gfkNQ=="
|
||||
},
|
||||
"backo2": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/backo2/-/backo2-1.0.2.tgz",
|
||||
"integrity": "sha1-MasayLEpNjRj41s+u2n038+6eUc="
|
||||
},
|
||||
"base64-arraybuffer": {
|
||||
"version": "0.1.4",
|
||||
"resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.4.tgz",
|
||||
@@ -41,43 +41,24 @@
|
||||
"resolved": "https://registry.npmjs.org/base64id/-/base64id-2.0.0.tgz",
|
||||
"integrity": "sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog=="
|
||||
},
|
||||
"better-assert": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://registry.npmjs.org/better-assert/-/better-assert-1.0.2.tgz",
|
||||
"integrity": "sha1-QIZrnhueC1W0gYlDEeaPr/rrxSI=",
|
||||
"requires": {
|
||||
"callsite": "1.0.0"
|
||||
}
|
||||
},
|
||||
"blob": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/blob/-/blob-0.0.5.tgz",
|
||||
"integrity": "sha512-gaqbzQPqOoamawKg0LGVd7SzLgXS+JH61oWprSLH+P+abTczqJbhTR8CmJ2u9/bUYNmHTGJx/UEmn6doAvvuig=="
|
||||
},
|
||||
"callsite": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/callsite/-/callsite-1.0.0.tgz",
|
||||
"integrity": "sha1-KAOY5dZkvXQDi28JBRU+borxvCA="
|
||||
},
|
||||
"component-bind": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/component-bind/-/component-bind-1.0.0.tgz",
|
||||
"integrity": "sha1-AMYIq33Nk4l8AAllGx06jh5zu9E="
|
||||
},
|
||||
"component-emitter": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.1.tgz",
|
||||
"integrity": "sha1-E3kY1teCg/ffemt8WmPhQOaUJeY="
|
||||
},
|
||||
"component-inherit": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/component-inherit/-/component-inherit-0.0.3.tgz",
|
||||
"integrity": "sha1-ZF/ErfWLcrZJ1crmUTVhnbJv8UM="
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",
|
||||
"integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg=="
|
||||
},
|
||||
"cookie": {
|
||||
"version": "0.3.1",
|
||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz",
|
||||
"integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s="
|
||||
"version": "0.4.1",
|
||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz",
|
||||
"integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA=="
|
||||
},
|
||||
"cors": {
|
||||
"version": "2.8.5",
|
||||
"resolved": "https://registry.npmjs.org/cors/-/cors-2.8.5.tgz",
|
||||
"integrity": "sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==",
|
||||
"requires": {
|
||||
"object-assign": "^4",
|
||||
"vary": "^1"
|
||||
}
|
||||
},
|
||||
"debug": {
|
||||
"version": "4.1.1",
|
||||
@@ -88,109 +69,27 @@
|
||||
}
|
||||
},
|
||||
"engine.io": {
|
||||
"version": "3.4.2",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-3.4.2.tgz",
|
||||
"integrity": "sha512-b4Q85dFkGw+TqgytGPrGgACRUhsdKc9S9ErRAXpPGy/CXKs4tYoHDkvIRdsseAF7NjfVwjRFIn6KTnbw7LwJZg==",
|
||||
"version": "4.0.5",
|
||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-4.0.5.tgz",
|
||||
"integrity": "sha512-Ri+whTNr2PKklxQkfbGjwEo+kCBUM4Qxk4wtLqLrhH+b1up2NFL9g9pjYWiCV/oazwB0rArnvF/ZmZN2ab5Hpg==",
|
||||
"requires": {
|
||||
"accepts": "~1.3.4",
|
||||
"base64id": "2.0.0",
|
||||
"cookie": "0.3.1",
|
||||
"cookie": "~0.4.1",
|
||||
"cors": "~2.8.5",
|
||||
"debug": "~4.1.0",
|
||||
"engine.io-parser": "~2.2.0",
|
||||
"engine.io-parser": "~4.0.0",
|
||||
"ws": "^7.1.2"
|
||||
}
|
||||
},
|
||||
"engine.io-client": {
|
||||
"version": "3.4.4",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-client/-/engine.io-client-3.4.4.tgz",
|
||||
"integrity": "sha512-iU4CRr38Fecj8HoZEnFtm2EiKGbYZcPn3cHxqNGl/tmdWRf60KhK+9vE0JeSjgnlS/0oynEfLgKbT9ALpim0sQ==",
|
||||
"requires": {
|
||||
"component-emitter": "~1.3.0",
|
||||
"component-inherit": "0.0.3",
|
||||
"debug": "~3.1.0",
|
||||
"engine.io-parser": "~2.2.0",
|
||||
"has-cors": "1.1.0",
|
||||
"indexof": "0.0.1",
|
||||
"parseqs": "0.0.6",
|
||||
"parseuri": "0.0.6",
|
||||
"ws": "~6.1.0",
|
||||
"xmlhttprequest-ssl": "~1.5.4",
|
||||
"yeast": "0.1.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"component-emitter": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",
|
||||
"integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg=="
|
||||
},
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
},
|
||||
"parseqs": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/parseqs/-/parseqs-0.0.6.tgz",
|
||||
"integrity": "sha512-jeAGzMDbfSHHA091hr0r31eYfTig+29g3GKKE/PPbEQ65X0lmMwlEoqmhzu0iztID5uJpZsFlUPDP8ThPL7M8w=="
|
||||
},
|
||||
"parseuri": {
|
||||
"version": "0.0.6",
|
||||
"resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.6.tgz",
|
||||
"integrity": "sha512-AUjen8sAkGgao7UyCX6Ahv0gIK2fABKmYjvP4xmy5JaKvcbTRueIqIPHLAfq30xJddqSE033IOMUSOMCcK3Sow=="
|
||||
},
|
||||
"ws": {
|
||||
"version": "6.1.4",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-6.1.4.tgz",
|
||||
"integrity": "sha512-eqZfL+NE/YQc1/ZynhojeV8q+H050oR8AZ2uIev7RU10svA9ZnJUddHcOUZTJLinZ9yEfdA2kSATS2qZK5fhJA==",
|
||||
"requires": {
|
||||
"async-limiter": "~1.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"engine.io-parser": {
|
||||
"version": "2.2.1",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-2.2.1.tgz",
|
||||
"integrity": "sha512-x+dN/fBH8Ro8TFwJ+rkB2AmuVw9Yu2mockR/p3W8f8YtExwFgDvBDi0GWyb4ZLkpahtDGZgtr3zLovanJghPqg==",
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/engine.io-parser/-/engine.io-parser-4.0.2.tgz",
|
||||
"integrity": "sha512-sHfEQv6nmtJrq6TKuIz5kyEKH/qSdK56H/A+7DnAuUPWosnIZAS2NHNcPLmyjtY3cGS/MqJdZbUjW97JU72iYg==",
|
||||
"requires": {
|
||||
"after": "0.8.2",
|
||||
"arraybuffer.slice": "~0.0.7",
|
||||
"base64-arraybuffer": "0.1.4",
|
||||
"blob": "0.0.5",
|
||||
"has-binary2": "~1.0.2"
|
||||
"base64-arraybuffer": "0.1.4"
|
||||
}
|
||||
},
|
||||
"has-binary2": {
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://registry.npmjs.org/has-binary2/-/has-binary2-1.0.3.tgz",
|
||||
"integrity": "sha512-G1LWKhDSvhGeAQ8mPVQlqNcOB2sJdwATtZKl2pDKKHfpf/rYj24lkinxf69blJbnsvtqqNU+L3SL50vzZhXOnw==",
|
||||
"requires": {
|
||||
"isarray": "2.0.1"
|
||||
}
|
||||
},
|
||||
"has-cors": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/has-cors/-/has-cors-1.1.0.tgz",
|
||||
"integrity": "sha1-XkdHk/fqmEPRu5nCPu9J/xJv/zk="
|
||||
},
|
||||
"indexof": {
|
||||
"version": "0.0.1",
|
||||
"resolved": "https://registry.npmjs.org/indexof/-/indexof-0.0.1.tgz",
|
||||
"integrity": "sha1-gtwzbSMrkGIXnQWrMpOmYFn9Q10="
|
||||
},
|
||||
"isarray": {
|
||||
"version": "2.0.1",
|
||||
"resolved": "https://registry.npmjs.org/isarray/-/isarray-2.0.1.tgz",
|
||||
"integrity": "sha1-o32U7ZzaLVmGXJ92/llu4fM4dB4="
|
||||
},
|
||||
"mime-db": {
|
||||
"version": "1.44.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz",
|
||||
@@ -214,132 +113,51 @@
|
||||
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
|
||||
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
|
||||
},
|
||||
"object-component": {
|
||||
"version": "0.0.3",
|
||||
"resolved": "https://registry.npmjs.org/object-component/-/object-component-0.0.3.tgz",
|
||||
"integrity": "sha1-8MaapQ78lbhmwYb0AKM3acsvEpE="
|
||||
},
|
||||
"parseqs": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/parseqs/-/parseqs-0.0.5.tgz",
|
||||
"integrity": "sha1-1SCKNzjkZ2bikbouoXNoSSGouJ0=",
|
||||
"requires": {
|
||||
"better-assert": "~1.0.0"
|
||||
}
|
||||
},
|
||||
"parseuri": {
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/parseuri/-/parseuri-0.0.5.tgz",
|
||||
"integrity": "sha1-gCBKUNTbt3m/3G6+J3jZDkvOMgo=",
|
||||
"requires": {
|
||||
"better-assert": "~1.0.0"
|
||||
}
|
||||
"object-assign": {
|
||||
"version": "4.1.1",
|
||||
"resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz",
|
||||
"integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM="
|
||||
},
|
||||
"socket.io": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-2.3.0.tgz",
|
||||
"integrity": "sha512-2A892lrj0GcgR/9Qk81EaY2gYhCBxurV0PfmmESO6p27QPrUK1J3zdns+5QPqvUYK2q657nSj0guoIil9+7eFg==",
|
||||
"version": "3.0.4",
|
||||
"resolved": "https://registry.npmjs.org/socket.io/-/socket.io-3.0.4.tgz",
|
||||
"integrity": "sha512-Vj1jUoO75WGc9txWd311ZJJqS9Dr8QtNJJ7gk2r7dcM/yGe9sit7qOijQl3GAwhpBOz/W8CwkD7R6yob07nLbA==",
|
||||
"requires": {
|
||||
"@types/cookie": "^0.4.0",
|
||||
"@types/cors": "^2.8.8",
|
||||
"@types/node": "^14.14.7",
|
||||
"accepts": "~1.3.4",
|
||||
"base64id": "~2.0.0",
|
||||
"debug": "~4.1.0",
|
||||
"engine.io": "~3.4.0",
|
||||
"has-binary2": "~1.0.2",
|
||||
"socket.io-adapter": "~1.1.0",
|
||||
"socket.io-client": "2.3.0",
|
||||
"socket.io-parser": "~3.4.0"
|
||||
"engine.io": "~4.0.0",
|
||||
"socket.io-adapter": "~2.0.3",
|
||||
"socket.io-parser": "~4.0.1"
|
||||
}
|
||||
},
|
||||
"socket.io-adapter": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-1.1.2.tgz",
|
||||
"integrity": "sha512-WzZRUj1kUjrTIrUKpZLEzFZ1OLj5FwLlAFQs9kuZJzJi5DKdU7FsWc36SNmA8iDOtwBQyT8FkrriRM8vXLYz8g=="
|
||||
},
|
||||
"socket.io-client": {
|
||||
"version": "2.3.0",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-client/-/socket.io-client-2.3.0.tgz",
|
||||
"integrity": "sha512-cEQQf24gET3rfhxZ2jJ5xzAOo/xhZwK+mOqtGRg5IowZsMgwvHwnf/mCRapAAkadhM26y+iydgwsXGObBB5ZdA==",
|
||||
"requires": {
|
||||
"backo2": "1.0.2",
|
||||
"base64-arraybuffer": "0.1.5",
|
||||
"component-bind": "1.0.0",
|
||||
"component-emitter": "1.2.1",
|
||||
"debug": "~4.1.0",
|
||||
"engine.io-client": "~3.4.0",
|
||||
"has-binary2": "~1.0.2",
|
||||
"has-cors": "1.1.0",
|
||||
"indexof": "0.0.1",
|
||||
"object-component": "0.0.3",
|
||||
"parseqs": "0.0.5",
|
||||
"parseuri": "0.0.5",
|
||||
"socket.io-parser": "~3.3.0",
|
||||
"to-array": "0.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"base64-arraybuffer": {
|
||||
"version": "0.1.5",
|
||||
"resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.1.5.tgz",
|
||||
"integrity": "sha1-c5JncZI7Whl0etZmqlzUv5xunOg="
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.0.0",
|
||||
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
|
||||
"integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g="
|
||||
"version": "2.0.3",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-adapter/-/socket.io-adapter-2.0.3.tgz",
|
||||
"integrity": "sha512-2wo4EXgxOGSFueqvHAdnmi5JLZzWqMArjuP4nqC26AtLh5PoCPsaRbRdah2xhcwTAMooZfjYiNVNkkmmSMaxOQ=="
|
||||
},
|
||||
"socket.io-parser": {
|
||||
"version": "3.3.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.3.1.tgz",
|
||||
"integrity": "sha512-1QLvVAe8dTz+mKmZ07Swxt+LAo4Y1ff50rlyoEx00TQmDFVQYPfcqGvIDJLGaBdhdNCecXtyKpD+EgKGcmmbuQ==",
|
||||
"version": "4.0.2",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-4.0.2.tgz",
|
||||
"integrity": "sha512-Bs3IYHDivwf+bAAuW/8xwJgIiBNtlvnjYRc4PbXgniLmcP1BrakBoq/QhO24rgtgW7VZ7uAaswRGxutUnlAK7g==",
|
||||
"requires": {
|
||||
"@types/component-emitter": "^1.2.10",
|
||||
"component-emitter": "~1.3.0",
|
||||
"debug": "~3.1.0",
|
||||
"isarray": "2.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"component-emitter": {
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.0.tgz",
|
||||
"integrity": "sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg=="
|
||||
},
|
||||
"debug": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz",
|
||||
"integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==",
|
||||
"requires": {
|
||||
"ms": "2.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
"debug": "~4.1.0"
|
||||
}
|
||||
},
|
||||
"socket.io-parser": {
|
||||
"version": "3.4.1",
|
||||
"resolved": "https://registry.npmjs.org/socket.io-parser/-/socket.io-parser-3.4.1.tgz",
|
||||
"integrity": "sha512-11hMgzL+WCLWf1uFtHSNvliI++tcRUWdoeYuwIl+Axvwy9z2gQM+7nJyN3STj1tLj5JyIUH8/gpDGxzAlDdi0A==",
|
||||
"requires": {
|
||||
"component-emitter": "1.2.1",
|
||||
"debug": "~4.1.0",
|
||||
"isarray": "2.0.1"
|
||||
}
|
||||
},
|
||||
"to-array": {
|
||||
"version": "0.1.4",
|
||||
"resolved": "https://registry.npmjs.org/to-array/-/to-array-0.1.4.tgz",
|
||||
"integrity": "sha1-F+bBH3PdTz10zaek/zI46a2b+JA="
|
||||
"vary": {
|
||||
"version": "1.1.2",
|
||||
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
|
||||
"integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw="
|
||||
},
|
||||
"ws": {
|
||||
"version": "7.4.1",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.1.tgz",
|
||||
"integrity": "sha512-pTsP8UAfhy3sk1lSk/O/s4tjD0CRwvMnzvwr4OKGX7ZvqZtUyx4KIJB5JWbkykPoc55tixMGgTNoh3k4FkNGFQ=="
|
||||
},
|
||||
"xmlhttprequest-ssl": {
|
||||
"version": "1.5.5",
|
||||
"resolved": "https://registry.npmjs.org/xmlhttprequest-ssl/-/xmlhttprequest-ssl-1.5.5.tgz",
|
||||
"integrity": "sha1-wodrBhaKrcQOV9l+gRkayPQ5iz4="
|
||||
},
|
||||
"yeast": {
|
||||
"version": "0.1.2",
|
||||
"resolved": "https://registry.npmjs.org/yeast/-/yeast-0.1.2.tgz",
|
||||
"integrity": "sha1-AI4G2AlDIMNy28L47XagymyKxBk="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"private": true,
|
||||
"dependencies": {
|
||||
"socket.io": "^2.3.0"
|
||||
"socket.io": "^3.0.4"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,12 @@ io.of('/abc').on('connection', function(socket) {
|
||||
socket.emit('handshake', socket.handshake);
|
||||
});
|
||||
|
||||
io.of("/no").use((socket, next) => {
|
||||
const err = new Error("auth failed");
|
||||
err.data = { a: "b", c: 3 };
|
||||
next(err);
|
||||
});
|
||||
|
||||
io.of(nsp).on('connection', function(socket) {
|
||||
socket.send('hello client');
|
||||
|
||||
@@ -88,8 +94,8 @@ io.of(nsp).on('connection', function(socket) {
|
||||
socket.broadcast.emit.apply(socket, ['broadcastBack'].concat(args));
|
||||
});
|
||||
|
||||
socket.on('room', (...args) => {
|
||||
io.to(socket.id).emit.apply(io.sockets, ['roomBack'].concat(args));
|
||||
socket.on('room', (arg) => {
|
||||
io.to(socket.id).emit("roomBack", arg);
|
||||
});
|
||||
|
||||
socket.on('requestDisconnect', function() {
|
||||
|
||||
Reference in New Issue
Block a user