diff --git a/History.md b/History.md
index f11dc8f8..be07a7ca 100644
--- a/History.md
+++ b/History.md
@@ -1,4 +1,19 @@
+1.0.2 / 2022-07-11
+==================
+
+From the "1.x" branch.
+
+### Bug Fixes
+
+* ensure buffered events are sent in order ([8bd35da](https://github.com/socketio/socket.io-client-java/commit/8bd35da19c1314318fe122876d22e30ae3673ff9))
+* ensure randomizationFactor is always between 0 and 1 ([cb966d5](https://github.com/socketio/socket.io-client-java/commit/cb966d5a64790c0584ad97cf55c205cae8bd1287))
+* ensure the payload format is valid ([8664499](https://github.com/socketio/socket.io-client-java/commit/8664499b6f31154f49783531f778dac5387b766b))
+* fix usage with ws:// scheme ([e57160a](https://github.com/socketio/socket.io-client-java/commit/e57160a00ca1fbb38396effdbc87eb10d6759a51))
+* increase the readTimeout value of the default OkHttpClient ([2d87497](https://github.com/socketio/engine.io-client-java/commit/2d874971c2428a7a444b3a33afe66aedcdce3a96)) (from `engine.io-client`)
+
+
+
1.0.1 / 2020-12-10
==================
diff --git a/README.md b/README.md
index 5b7af7d6..50efc96a 100644
--- a/README.md
+++ b/README.md
@@ -49,7 +49,7 @@ Add the following dependency to your `pom.xml`.
Add it as a gradle dependency for Android Studio, in `build.gradle`:
```groovy
-compile ('io.socket:socket.io-client:1.0.1') {
+compile ('io.socket:socket.io-client:1.0.2') {
// excluding org.json which is provided by Android
exclude group: 'org.json', module: 'json'
}
diff --git a/pom.xml b/pom.xml
index 4a3d2e3e..ace14ef2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
4.0.0
io.socket
socket.io-client
- 1.0.2-SNAPSHOT
+ 1.0.3-SNAPSHOT
jar
socket.io-client
Socket.IO Client Library for Java
@@ -62,7 +62,7 @@
io.socket
engine.io-client
- 1.0.1
+ 1.0.2
org.json
diff --git a/src/main/java/io/socket/backo/Backoff.java b/src/main/java/io/socket/backo/Backoff.java
index f5199213..81e1dddd 100644
--- a/src/main/java/io/socket/backo/Backoff.java
+++ b/src/main/java/io/socket/backo/Backoff.java
@@ -3,6 +3,9 @@
import java.math.BigDecimal;
import java.math.BigInteger;
+/**
+ * Imported from https://github.com/mokesmokes/backo
+ */
public class Backoff {
private long ms = 100;
@@ -23,7 +26,10 @@ public long duration() {
.multiply(new BigDecimal(ms)).toBigInteger();
ms = (((int) Math.floor(rand * 10)) & 1) == 0 ? ms.subtract(deviation) : ms.add(deviation);
}
- return ms.min(BigInteger.valueOf(this.max)).longValue();
+ return ms
+ .min(BigInteger.valueOf(this.max))
+ .max(BigInteger.valueOf(this.ms))
+ .longValue();
}
public void reset() {
@@ -46,6 +52,10 @@ public Backoff setFactor(int factor) {
}
public Backoff setJitter(double jitter) {
+ boolean isValid = jitter >= 0 && jitter < 1;
+ if (!isValid) {
+ throw new IllegalArgumentException("jitter must be between 0 and 1");
+ }
this.jitter = jitter;
return this;
}
diff --git a/src/main/java/io/socket/client/IO.java b/src/main/java/io/socket/client/IO.java
index a5455f48..69637ec5 100644
--- a/src/main/java/io/socket/client/IO.java
+++ b/src/main/java/io/socket/client/IO.java
@@ -7,7 +7,6 @@
import java.net.URI;
import java.net.URISyntaxException;
-import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -58,17 +57,12 @@ public static Socket socket(URI uri, Options opts) {
opts = new Options();
}
- URL parsed = Url.parse(uri);
- URI source;
- try {
- source = parsed.toURI();
- } catch (URISyntaxException e) {
- throw new RuntimeException(e);
- }
- String id = Url.extractId(parsed);
- String path = parsed.getPath();
+ Url.ParsedURI parsed = Url.parse(uri);
+ URI source = parsed.uri;
+ String id = parsed.id;
+
boolean sameNamespace = managers.containsKey(id)
- && managers.get(id).nsps.containsKey(path);
+ && managers.get(id).nsps.containsKey(source.getPath());
boolean newConnection = opts.forceNew || !opts.multiplex || sameNamespace;
Manager io;
@@ -87,12 +81,12 @@ public static Socket socket(URI uri, Options opts) {
io = managers.get(id);
}
- String query = parsed.getQuery();
+ String query = source.getQuery();
if (query != null && (opts.query == null || opts.query.isEmpty())) {
opts.query = query;
}
- return io.socket(parsed.getPath(), opts);
+ return io.socket(source.getPath(), opts);
}
diff --git a/src/main/java/io/socket/client/Manager.java b/src/main/java/io/socket/client/Manager.java
index 1058b067..cac32bf7 100644
--- a/src/main/java/io/socket/client/Manager.java
+++ b/src/main/java/io/socket/client/Manager.java
@@ -2,6 +2,7 @@
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;
@@ -370,10 +371,14 @@ private void onopen() {
@Override
public void call(Object... objects) {
Object data = objects[0];
- if (data instanceof String) {
- Manager.this.ondata((String)data);
- } else if (data instanceof byte[]) {
- Manager.this.ondata((byte[])data);
+ try {
+ if (data instanceof String) {
+ Manager.this.decoder.add((String) data);
+ } else if (data instanceof byte[]) {
+ Manager.this.decoder.add((byte[]) data);
+ }
+ } catch (DecodingException e) {
+ logger.fine("error while decoding the packet: " + e.getMessage());
}
}
}));
@@ -419,14 +424,6 @@ private void onpong() {
null != this.lastPing ? new Date().getTime() - this.lastPing.getTime() : 0);
}
- private void ondata(String data) {
- this.decoder.add(data);
- }
-
- private void ondata(byte[] data) {
- this.decoder.add(data);
- }
-
private void ondecoded(Packet packet) {
this.emit(EVENT_PACKET, packet);
}
diff --git a/src/main/java/io/socket/client/Socket.java b/src/main/java/io/socket/client/Socket.java
index 369d6244..71ad6f01 100644
--- a/src/main/java/io/socket/client/Socket.java
+++ b/src/main/java/io/socket/client/Socket.java
@@ -386,8 +386,8 @@ private void onack(Packet packet) {
private void onconnect() {
this.connected = true;
- this.emit(EVENT_CONNECT);
this.emitBuffered();
+ super.emit(EVENT_CONNECT);
}
private void emitBuffered() {
diff --git a/src/main/java/io/socket/client/Url.java b/src/main/java/io/socket/client/Url.java
index c9185d29..451eee8b 100644
--- a/src/main/java/io/socket/client/Url.java
+++ b/src/main/java/io/socket/client/Url.java
@@ -1,16 +1,11 @@
package io.socket.client;
-import java.net.MalformedURLException;
import java.net.URI;
-import java.net.URISyntaxException;
-import java.net.URL;
-import java.util.regex.Pattern;
import java.util.regex.Matcher;
+import java.util.regex.Pattern;
public class Url {
- private static Pattern PATTERN_HTTP = Pattern.compile("^http|ws$");
- private static Pattern PATTERN_HTTPS = Pattern.compile("^(http|ws)s$");
/**
* Expected format: "[id:password@]host[:port]"
*/
@@ -18,11 +13,17 @@ public class Url {
private Url() {}
- public static URL parse(String uri) throws URISyntaxException {
- return parse(new URI(uri));
+ static class ParsedURI {
+ public final URI uri;
+ public final String id;
+
+ public ParsedURI(URI uri, String id) {
+ this.uri = uri;
+ this.id = id;
+ }
}
- public static URL parse(URI uri) {
+ public static ParsedURI parse(URI uri) {
String protocol = uri.getScheme();
if (protocol == null || !protocol.matches("^https?|wss?$")) {
protocol = "https";
@@ -30,9 +31,9 @@ public static URL parse(URI uri) {
int port = uri.getPort();
if (port == -1) {
- if (PATTERN_HTTP.matcher(protocol).matches()) {
+ if ("http".equals(protocol) || "ws".equals(protocol)) {
port = 80;
- } else if (PATTERN_HTTPS.matcher(protocol).matches()) {
+ } else if ("https".equals(protocol) || "wss".equals(protocol)) {
port = 443;
}
}
@@ -50,35 +51,18 @@ public static URL parse(URI uri) {
// might happen on some of Samsung Devices such as S4.
_host = extractHostFromAuthorityPart(uri.getRawAuthority());
}
- try {
- return new URL(protocol + "://"
- + (userInfo != null ? userInfo + "@" : "")
- + _host
- + (port != -1 ? ":" + port : "")
- + path
- + (query != null ? "?" + query : "")
- + (fragment != null ? "#" + fragment : ""));
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- }
-
- public static String extractId(String url) throws MalformedURLException {
- return extractId(new URL(url));
+ URI completeUri = URI.create(protocol + "://"
+ + (userInfo != null ? userInfo + "@" : "")
+ + _host
+ + (port != -1 ? ":" + port : "")
+ + path
+ + (query != null ? "?" + query : "")
+ + (fragment != null ? "#" + fragment : ""));
+ String id = protocol + "://" + _host + ":" + port;
+
+ return new ParsedURI(completeUri, id);
}
- public static String extractId(URL url) {
- String protocol = url.getProtocol();
- int port = url.getPort();
- if (port == -1) {
- if (PATTERN_HTTP.matcher(protocol).matches()) {
- port = 80;
- } else if (PATTERN_HTTPS.matcher(protocol).matches()) {
- port = 443;
- }
- }
- return protocol + "://" + url.getHost() + ":" + port;
- }
private static String extractHostFromAuthorityPart(String authority)
{
diff --git a/src/main/java/io/socket/parser/DecodingException.java b/src/main/java/io/socket/parser/DecodingException.java
new file mode 100644
index 00000000..04dc0448
--- /dev/null
+++ b/src/main/java/io/socket/parser/DecodingException.java
@@ -0,0 +1,7 @@
+package io.socket.parser;
+
+public class DecodingException extends RuntimeException {
+ public DecodingException(String message) {
+ super(message);
+ }
+}
diff --git a/src/main/java/io/socket/parser/IOParser.java b/src/main/java/io/socket/parser/IOParser.java
index 813c16ca..32c6b49d 100644
--- a/src/main/java/io/socket/parser/IOParser.java
+++ b/src/main/java/io/socket/parser/IOParser.java
@@ -1,7 +1,9 @@
package io.socket.parser;
import io.socket.hasbinary.HasBinary;
+import org.json.JSONArray;
import org.json.JSONException;
+import org.json.JSONObject;
import org.json.JSONTokener;
import java.util.ArrayList;
@@ -14,10 +16,6 @@ final public class IOParser implements Parser {
private static final Logger logger = Logger.getLogger(IOParser.class.getName());
- private static Packet error() {
- return new Packet(ERROR, "parser error");
- }
-
private IOParser() {}
final public static class Encoder implements Parser.Encoder {
@@ -126,12 +124,16 @@ private static Packet decodeString(String str) {
int i = 0;
int length = str.length();
- Packet