From 615942b8287c5e5cbedd48209116f601e32289f1 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Fri, 19 Mar 2021 14:54:16 +0100 Subject: [PATCH 01/15] docs: update compatibility table with Socket.IO v4 There is no breaking change at the protocol level. Reference: https://socket.io/blog/socket-io-4-release/ --- src/site/markdown/installation.md | 4 ++-- src/site/markdown/migrating_from_1_x.md | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/site/markdown/installation.md b/src/site/markdown/installation.md index 8f9a2d9f..1356a629 100644 --- a/src/site/markdown/installation.md +++ b/src/site/markdown/installation.md @@ -3,8 +3,8 @@ | Client version | Socket.IO server | | -------------- | ---------------- | | 0.9.x | 1.x | -| 1.x | 2.x | -| 2.x | 3.x | +| 1.x | 2.x (or 3.1.x / 4.x with [`allowEIO3: true`](https://socket.io/docs/v4/server-initialization/#allowEIO3)) | +| 2.x | 3.x / 4.x | ## Installation The latest artifact is available on Maven Central. diff --git a/src/site/markdown/migrating_from_1_x.md b/src/site/markdown/migrating_from_1_x.md index c9d7c0cc..f13bebf3 100644 --- a/src/site/markdown/migrating_from_1_x.md +++ b/src/site/markdown/migrating_from_1_x.md @@ -7,8 +7,8 @@ Here is the compatibility table: | Java client version | Socket.IO server | | -------------- | ---------------- | | 0.9.x | 1.x | -| 1.x | 2.x | -| 2.x | 3.x | +| 1.x | 2.x (or 3.1.x / 4.x with [`allowEIO3: true`](https://socket.io/docs/v4/server-initialization/#allowEIO3)) | +| 2.x | 3.x / 4.x | **Important note:** due to the backward incompatible changes to the Socket.IO protocol, a 2.x Java client will not be able to reach a 2.x server, and vice-versa From 5b5b91cb016131147ffd75b7fa0ae0a3d34c2bca Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 09:12:55 +0200 Subject: [PATCH 02/15] test: fix random test failures --- src/main/java/io/socket/client/Manager.java | 27 ++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/main/java/io/socket/client/Manager.java b/src/main/java/io/socket/client/Manager.java index 67c2f704..089a2e56 100644 --- a/src/main/java/io/socket/client/Manager.java +++ b/src/main/java/io/socket/client/Manager.java @@ -271,23 +271,28 @@ public void call(Object... objects) { } }); - if (Manager.this._timeout >= 0) { - final long timeout = Manager.this._timeout; + final long timeout = Manager.this._timeout; + final Runnable onTimeout = new Runnable() { + @Override + public void run() { + logger.fine(String.format("connect attempt timed out after %d", timeout)); + openSub.destroy(); + socket.close(); + socket.emit(Engine.EVENT_ERROR, new SocketIOException("timeout")); + } + }; + + if (timeout == 0) { + EventThread.exec(onTimeout); + return; + } else if (Manager.this._timeout > 0) { logger.fine(String.format("connection attempt will timeout after %d", timeout)); final Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { - EventThread.exec(new Runnable() { - @Override - public void run() { - logger.fine(String.format("connect attempt timed out after %d", timeout)); - openSub.destroy(); - socket.close(); - socket.emit(Engine.EVENT_ERROR, new SocketIOException("timeout")); - } - }); + EventThread.exec(onTimeout); } }, timeout); From e2e24ea75dacce16318bcfa88db8927d96cb0ec1 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 09:22:05 +0200 Subject: [PATCH 03/15] docs: update compatibility table with Socket.IO v4 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4eed16b5..28c73c09 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ See also: | -------------- | ---------------- | | 0.9.x | 1.x | | 1.x | 2.x | -| 2.x | 3.x | +| 2.x | 3.x / 4.x | ## Documentation From 67fd5f34a31c63f7884f82ab39386ad343527590 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 10:27:29 +0200 Subject: [PATCH 04/15] fix: fix usage with ws:// scheme The URL constructor does not support the ws:// scheme, and would throw: > java.net.MalformedURLException: unknown protocol: ws Related: - https://github.com/socketio/socket.io-client-java/issues/650 - https://github.com/socketio/socket.io-client-java/issues/555 - https://github.com/socketio/socket.io-client-java/issues/233 --- src/main/java/io/socket/client/IO.java | 20 ++--- src/main/java/io/socket/client/Url.java | 60 ++++++--------- src/test/java/io/socket/client/UrlTest.java | 85 +++++++++++++-------- 3 files changed, 84 insertions(+), 81 deletions(-) diff --git a/src/main/java/io/socket/client/IO.java b/src/main/java/io/socket/client/IO.java index e307b6e0..0423f27e 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,21 +57,16 @@ 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; - String query = parsed.getQuery(); + String query = source.getQuery(); if (query != null && (opts.query == null || opts.query.isEmpty())) { opts.query = query; } @@ -92,7 +86,7 @@ public static Socket socket(URI uri, Options opts) { io = managers.get(id); } - return io.socket(parsed.getPath(), opts); + return io.socket(source.getPath(), opts); } 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/test/java/io/socket/client/UrlTest.java b/src/test/java/io/socket/client/UrlTest.java index fbcf42de..47a1a0c1 100644 --- a/src/test/java/io/socket/client/UrlTest.java +++ b/src/test/java/io/socket/client/UrlTest.java @@ -4,9 +4,7 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.net.MalformedURLException; -import java.net.URISyntaxException; -import java.net.URL; +import java.net.URI; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; @@ -15,58 +13,85 @@ @RunWith(JUnit4.class) public class UrlTest { + private URI parse(String uri) { + return Url.parse(URI.create(uri)).uri; + } + + private String extractId(String uri) { + return Url.parse(URI.create(uri)).id; + } + @Test - public void parse() throws URISyntaxException { - assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(), + public void parse() { + assertThat(parse("http://username:password@host:8080/directory/file?query#ref").toString(), is("http://username:password@host:8080/directory/file?query#ref")); } @Test - public void parseRelativePath() throws URISyntaxException { - URL url = Url.parse("https://woot.com/test"); - assertThat(url.getProtocol(), is("https")); - assertThat(url.getHost(), is("woot.com")); - assertThat(url.getPath(), is("/test")); + public void parseRelativePath() { + URI uri = parse("https://woot.com/test"); + assertThat(uri.getScheme(), is("https")); + assertThat(uri.getHost(), is("woot.com")); + assertThat(uri.getPath(), is("/test")); } @Test - public void parseNoProtocol() throws URISyntaxException { - URL url = Url.parse("//localhost:3000"); - assertThat(url.getProtocol(), is("https")); - assertThat(url.getHost(), is("localhost")); - assertThat(url.getPort(), is(3000)); + public void parseNoProtocol() { + URI uri = parse("//localhost:3000"); + assertThat(uri.getScheme(), is("https")); + assertThat(uri.getHost(), is("localhost")); + assertThat(uri.getPort(), is(3000)); } @Test - public void parseNamespace() throws URISyntaxException { - assertThat(Url.parse("http://woot.com/woot").getPath(), is("/woot")); - assertThat(Url.parse("http://google.com").getPath(), is("/")); - assertThat(Url.parse("http://google.com/").getPath(), is("/")); + public void parseNamespace() { + assertThat(parse("http://woot.com/woot").getPath(), is("/woot")); + assertThat(parse("http://google.com").getPath(), is("/")); + assertThat(parse("http://google.com/").getPath(), is("/")); } @Test - public void parseDefaultPort() throws URISyntaxException { - assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/")); - assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/")); + public void parseDefaultPort() { + assertThat(parse("http://google.com/").toString(), is("http://google.com:80/")); + assertThat(parse("https://google.com/").toString(), is("https://google.com:443/")); } @Test - public void extractId() throws MalformedURLException { - String id1 = Url.extractId("http://google.com:80/"); - String id2 = Url.extractId("http://google.com/"); - String id3 = Url.extractId("https://google.com/"); + public void testWsProtocol() { + URI uri = parse("ws://woot.com/test"); + assertThat(uri.getScheme(), is("ws")); + assertThat(uri.getHost(), is("woot.com")); + assertThat(uri.getPort(), is(80)); + assertThat(uri.getPath(), is("/test")); + } + + @Test + public void testWssProtocol() { + URI uri = parse("wss://woot.com/test"); + assertThat(uri.getScheme(), is("wss")); + assertThat(uri.getHost(), is("woot.com")); + assertThat(uri.getPort(), is(443)); + assertThat(uri.getPath(), is("/test")); + } + + @Test + public void extractId() { + String id1 = extractId("http://google.com:80/"); + String id2 = extractId("http://google.com/"); + String id3 = extractId("https://google.com/"); assertThat(id1, is(id2)); assertThat(id1, is(not(id3))); assertThat(id2, is(not(id3))); } @Test - public void ipv6() throws URISyntaxException, MalformedURLException { + public void ipv6() { String url = "http://[::1]"; - URL parsed = Url.parse(url); - assertThat(parsed.getProtocol(), is("http")); + URI parsed = parse(url); + assertThat(parsed.getScheme(), is("http")); assertThat(parsed.getHost(), is("[::1]")); assertThat(parsed.getPort(), is(80)); - assertThat(Url.extractId(url), is("http://[::1]:80")); + assertThat(extractId(url), is("http://[::1]:80")); } + } From a4053e864580fa90c46d496f421441c3bd8db6c6 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 10:48:41 +0200 Subject: [PATCH 05/15] test: cleanup URISyntaxException exceptions Note: we cannot update the `IO.socket(uri: string)` method without doing a breaking change. --- .../java/io/socket/client/Connection.java | 16 ++-- .../java/io/socket/client/ConnectionTest.java | 78 +++++++++---------- .../io/socket/client/SSLConnectionTest.java | 5 +- .../socket/client/ServerConnectionTest.java | 27 +++---- .../java/io/socket/client/SocketTest.java | 21 +++-- 5 files changed, 71 insertions(+), 76 deletions(-) diff --git a/src/test/java/io/socket/client/Connection.java b/src/test/java/io/socket/client/Connection.java index 9e7bf8c6..8b97bc22 100644 --- a/src/test/java/io/socket/client/Connection.java +++ b/src/test/java/io/socket/client/Connection.java @@ -6,7 +6,7 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; -import java.net.URISyntaxException; +import java.net.URI; import java.util.HashMap; import java.util.Map; import java.util.concurrent.*; @@ -77,24 +77,24 @@ public void stopServer() throws InterruptedException { serverService.awaitTermination(3000, TimeUnit.MILLISECONDS); } - Socket client() throws URISyntaxException { + Socket client() { return client(createOptions()); } - Socket client(String path) throws URISyntaxException { + Socket client(String path) { return client(path, createOptions()); } - Socket client(IO.Options opts) throws URISyntaxException { + Socket client(IO.Options opts) { return client(nsp(), opts); } - Socket client(String path, IO.Options opts) throws URISyntaxException { - return IO.socket(uri() + path, opts); + Socket client(String path, IO.Options opts) { + return IO.socket(URI.create(uri() + path), opts); } - String uri() { - return "http://localhost:" + PORT; + URI uri() { + return URI.create("http://localhost:" + PORT); } String nsp() { diff --git a/src/test/java/io/socket/client/ConnectionTest.java b/src/test/java/io/socket/client/ConnectionTest.java index ee0c9237..13ef7bff 100644 --- a/src/test/java/io/socket/client/ConnectionTest.java +++ b/src/test/java/io/socket/client/ConnectionTest.java @@ -29,7 +29,7 @@ public class ConnectionTest extends Connection { private Socket socket; @Test(timeout = TIMEOUT) - public void connectToLocalhost() throws URISyntaxException, InterruptedException { + public void connectToLocalhost() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -50,7 +50,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void startTwoConnectionsWithSamePath() throws URISyntaxException, InterruptedException { + public void startTwoConnectionsWithSamePath() throws InterruptedException { Socket s1 = client("/"); Socket s2 = client("/"); @@ -60,7 +60,7 @@ public void startTwoConnectionsWithSamePath() throws URISyntaxException, Interru } @Test(timeout = TIMEOUT) - public void startTwoConnectionsWithSamePathAndDifferentQuerystrings() throws URISyntaxException, InterruptedException { + public void startTwoConnectionsWithSamePathAndDifferentQuerystrings() throws InterruptedException { Socket s1 = client("/?woot"); Socket s2 = client("/"); @@ -70,7 +70,7 @@ public void startTwoConnectionsWithSamePathAndDifferentQuerystrings() throws URI } @Test(timeout = TIMEOUT) - public void workWithAcks() throws URISyntaxException, InterruptedException { + public void workWithAcks() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -111,7 +111,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void receiveDateWithAck() throws URISyntaxException, InterruptedException { + public void receiveDateWithAck() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -136,7 +136,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void sendBinaryAck() throws URISyntaxException, InterruptedException { + public void sendBinaryAck() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8")); @@ -168,7 +168,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void receiveBinaryDataWithAck() throws URISyntaxException, InterruptedException { + public void receiveBinaryDataWithAck() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8")); @@ -191,7 +191,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void workWithFalse() throws URISyntaxException, InterruptedException { + public void workWithFalse() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -212,7 +212,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void receiveUTF8MultibyteCharacters() throws URISyntaxException, InterruptedException { + public void receiveUTF8MultibyteCharacters() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); final String[] correct = new String[] { "てすと", @@ -245,9 +245,9 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void connectToNamespaceAfterConnectionEstablished() throws URISyntaxException, InterruptedException { + public void connectToNamespaceAfterConnectionEstablished() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); - final Manager manager = new Manager(new URI(uri())); + final Manager manager = new Manager(uri()); socket = manager.socket("/"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -270,9 +270,9 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void connectToNamespaceAfterConnectionGetsClosed() throws URISyntaxException, InterruptedException { + public void connectToNamespaceAfterConnectionGetsClosed() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); - final Manager manager = new Manager(new URI(uri())); + final Manager manager = new Manager(uri()); socket = manager.socket("/"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -299,7 +299,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void reconnectByDefault() throws URISyntaxException, InterruptedException { + public void reconnectByDefault() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.io().on(Manager.EVENT_RECONNECT, new Emitter.Listener() { @@ -320,7 +320,7 @@ public void run() { } @Test(timeout = TIMEOUT) - public void reconnectManually() throws URISyntaxException, InterruptedException { + public void reconnectManually() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -346,7 +346,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void reconnectAutomaticallyAfterReconnectingManually() throws URISyntaxException, InterruptedException { + public void reconnectAutomaticallyAfterReconnectingManually() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -378,14 +378,14 @@ public void run() { } @Test(timeout = 14000) - public void attemptReconnectsAfterAFailedReconnect() throws URISyntaxException, InterruptedException { + public void attemptReconnectsAfterAFailedReconnect() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = createOptions(); opts.reconnection = true; opts.timeout = 0; opts.reconnectionAttempts = 2; opts.reconnectionDelay = 10; - final Manager manager = new Manager(new URI(uri()), opts); + final Manager manager = new Manager(uri(), opts); socket = manager.socket("/timeout"); manager.once(Manager.EVENT_RECONNECT_FAILED, new Emitter.Listener() { @Override @@ -415,7 +415,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void reconnectDelayShouldIncreaseEveryTime() throws URISyntaxException, InterruptedException { + public void reconnectDelayShouldIncreaseEveryTime() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = createOptions(); opts.reconnection = true; @@ -423,7 +423,7 @@ public void reconnectDelayShouldIncreaseEveryTime() throws URISyntaxException, I opts.reconnectionAttempts = 3; opts.reconnectionDelay = 100; opts.randomizationFactor = 0.2; - final Manager manager = new Manager(new URI(uri()), opts); + final Manager manager = new Manager(uri(), opts); socket = manager.socket("/timeout"); final int[] reconnects = new int[] {0}; @@ -524,7 +524,7 @@ public void run() { } @Test(timeout = TIMEOUT) - public void reconnectAfterStoppingReconnection() throws URISyntaxException, InterruptedException { + public void reconnectAfterStoppingReconnection() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = createOptions(); opts.forceNew = true; @@ -550,9 +550,9 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void stopReconnectingOnASocketAndKeepToReconnectOnAnother() throws URISyntaxException, InterruptedException { + public void stopReconnectingOnASocketAndKeepToReconnectOnAnother() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); - final Manager manager = new Manager(new URI(uri())); + final Manager manager = new Manager(uri()); final Socket socket1 = manager.socket("/"); final Socket socket2 = manager.socket("/asd"); @@ -596,10 +596,10 @@ public void run() { } @Test(timeout = TIMEOUT) - public void connectWhileDisconnectingAnotherSocket() throws URISyntaxException, InterruptedException { + public void connectWhileDisconnectingAnotherSocket() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); - final Manager manager = new Manager(new URI(uri())); + final Manager manager = new Manager(uri()); final Socket socket1 = manager.socket("/foo"); socket1.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -623,13 +623,13 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void tryToReconnectTwiceAndFailWithIncorrectAddress() throws URISyntaxException, InterruptedException { + public void tryToReconnectTwiceAndFailWithIncorrectAddress() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = new IO.Options(); opts.reconnection = true; opts.reconnectionAttempts = 2; opts.reconnectionDelay = 10; - final Manager manager = new Manager(new URI("http://localhost:3940"), opts); + final Manager manager = new Manager(URI.create("http://localhost:3940"), opts); socket = manager.socket("/asd"); final int[] reconnects = new int[] {0}; Emitter.Listener cb = new Emitter.Listener() { @@ -655,14 +655,14 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void tryToReconnectTwiceAndFailWithImmediateTimeout() throws URISyntaxException, InterruptedException { + public void tryToReconnectTwiceAndFailWithImmediateTimeout() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = new IO.Options(); opts.reconnection = true; opts.timeout = 0; opts.reconnectionAttempts = 2; opts.reconnectionDelay = 10; - final Manager manager = new Manager(new URI(uri()), opts); + final Manager manager = new Manager(uri(), opts); final int[] reconnects = new int[] {0}; Emitter.Listener reconnectCb = new Emitter.Listener() { @@ -688,11 +688,11 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void notTryToReconnectWithIncorrectPortWhenReconnectionDisabled() throws URISyntaxException, InterruptedException { + public void notTryToReconnectWithIncorrectPortWhenReconnectionDisabled() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = new IO.Options(); opts.reconnection = false; - final Manager manager = new Manager(new URI("http://localhost:9823"), opts); + final Manager manager = new Manager(URI.create("http://localhost:9823"), opts); Emitter.Listener cb = new Emitter.Listener() { @Override public void call(Object... objects) { @@ -722,7 +722,7 @@ public void run() { } @Test(timeout = TIMEOUT) - public void fireReconnectEventsOnSocket() throws URISyntaxException, InterruptedException { + public void fireReconnectEventsOnSocket() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); Manager.Options opts = new Manager.Options(); @@ -730,7 +730,7 @@ public void fireReconnectEventsOnSocket() throws URISyntaxException, Interrupted opts.timeout = 0; opts.reconnectionAttempts = 2; opts.reconnectionDelay = 10; - final Manager manager = new Manager(new URI(uri()), opts); + final Manager manager = new Manager(uri(), opts); socket = manager.socket("/timeout_socket"); final int[] reconnects = new int[] {0}; @@ -757,7 +757,7 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void fireReconnectingWithAttemptsNumberWhenReconnectingTwice() throws URISyntaxException, InterruptedException { + public void fireReconnectingWithAttemptsNumberWhenReconnectingTwice() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); Manager.Options opts = new Manager.Options(); @@ -765,7 +765,7 @@ public void fireReconnectingWithAttemptsNumberWhenReconnectingTwice() throws URI opts.timeout = 0; opts.reconnectionAttempts = 2; opts.reconnectionDelay = 10; - final Manager manager = new Manager(new URI(uri()), opts); + final Manager manager = new Manager(uri(), opts); socket = manager.socket("/timeout_socket"); final int[] reconnects = new int[] {0}; @@ -792,7 +792,7 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void emitDateAsString() throws URISyntaxException, InterruptedException { + public void emitDateAsString() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -813,7 +813,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void emitDateInObject() throws URISyntaxException, InterruptedException, JSONException { + public void emitDateInObject() throws InterruptedException, JSONException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -843,7 +843,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) - public void sendAndGetBinaryData() throws URISyntaxException, InterruptedException { + public void sendAndGetBinaryData() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); final byte[] buf = "asdfasdf".getBytes(Charset.forName("UTF-8")); socket = client(); @@ -865,7 +865,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void sendBinaryDataMixedWithJson() throws URISyntaxException, InterruptedException, JSONException { + public void sendBinaryDataMixedWithJson() throws InterruptedException, JSONException { final BlockingQueue values = new LinkedBlockingQueue(); final byte[] buf = "howdy".getBytes(Charset.forName("UTF-8")); socket = client(); diff --git a/src/test/java/io/socket/client/SSLConnectionTest.java b/src/test/java/io/socket/client/SSLConnectionTest.java index 26ad1b10..2661f0c1 100644 --- a/src/test/java/io/socket/client/SSLConnectionTest.java +++ b/src/test/java/io/socket/client/SSLConnectionTest.java @@ -16,6 +16,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.net.URI; import java.security.GeneralSecurityException; import java.security.KeyStore; import java.util.concurrent.BlockingQueue; @@ -39,8 +40,8 @@ public class SSLConnectionTest extends Connection { } @Override - String uri() { - return "https://localhost:" + PORT; + URI uri() { + return URI.create("https://localhost:" + PORT); } @Override diff --git a/src/test/java/io/socket/client/ServerConnectionTest.java b/src/test/java/io/socket/client/ServerConnectionTest.java index 6c421a1b..9360b150 100644 --- a/src/test/java/io/socket/client/ServerConnectionTest.java +++ b/src/test/java/io/socket/client/ServerConnectionTest.java @@ -9,7 +9,6 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.net.URISyntaxException; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -26,7 +25,7 @@ public class ServerConnectionTest extends Connection { private Socket socket2; @Test(timeout = TIMEOUT) - public void openAndClose() throws URISyntaxException, InterruptedException { + public void openAndClose() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -51,7 +50,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void message() throws URISyntaxException, InterruptedException { + public void message() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -131,7 +130,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void ackWithoutArgs() throws URISyntaxException, InterruptedException { + public void ackWithoutArgs() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -153,7 +152,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void ackWithoutArgsFromClient() throws URISyntaxException, InterruptedException { + public void ackWithoutArgsFromClient() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -188,7 +187,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void closeEngineConnection() throws URISyntaxException, InterruptedException { + public void closeEngineConnection() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -209,18 +208,14 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void broadcast() throws URISyntaxException, InterruptedException { + public void broadcast() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override public void call(Object... objects) { - try { - socket2 = client(); - } catch (URISyntaxException e) { - throw new RuntimeException(e); - } + socket2 = client(); socket2.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -246,7 +241,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void room() throws URISyntaxException, InterruptedException { + public void room() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); @@ -270,7 +265,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void pollingHeaders() throws URISyntaxException, InterruptedException { + public void pollingHeaders() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = createOptions(); @@ -305,7 +300,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void websocketHandshakeHeaders() throws URISyntaxException, InterruptedException { + public void websocketHandshakeHeaders() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = createOptions(); @@ -340,7 +335,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void disconnectFromServer() throws URISyntaxException, InterruptedException { + public void disconnectFromServer() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); diff --git a/src/test/java/io/socket/client/SocketTest.java b/src/test/java/io/socket/client/SocketTest.java index 5851ff77..f710f5e2 100644 --- a/src/test/java/io/socket/client/SocketTest.java +++ b/src/test/java/io/socket/client/SocketTest.java @@ -8,7 +8,6 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import java.net.URISyntaxException; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.BlockingQueue; @@ -26,7 +25,7 @@ public class SocketTest extends Connection { private Socket socket; @Test(timeout = TIMEOUT) - public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws URISyntaxException, InterruptedException { + public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -45,7 +44,7 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws URISyntaxException, InterruptedException { + public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client("/foo"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -64,7 +63,7 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void clearsSocketIdUponDisconnection() throws URISyntaxException, InterruptedException { + public void clearsSocketIdUponDisconnection() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -87,7 +86,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws URISyntaxException, InterruptedException { + public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = new IO.Options(); opts.timeout = 100; @@ -114,7 +113,7 @@ public void run() { } @Test(timeout = TIMEOUT) - public void shouldChangeSocketIdUponReconnection() throws URISyntaxException, InterruptedException { + public void shouldChangeSocketIdUponReconnection() throws InterruptedException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client(); socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -155,7 +154,7 @@ public void call(Object... objects) { } @Test(timeout = TIMEOUT) - public void shouldAcceptAQueryStringOnDefaultNamespace() throws URISyntaxException, InterruptedException, JSONException { + public void shouldAcceptAQueryStringOnDefaultNamespace() throws InterruptedException, JSONException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client("/?c=d"); @@ -176,7 +175,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void shouldAcceptAQueryString() throws URISyntaxException, InterruptedException, JSONException { + public void shouldAcceptAQueryString() throws InterruptedException, JSONException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client("/abc?b=c&d=e"); @@ -199,7 +198,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void shouldAcceptAnAuthOption() throws URISyntaxException, InterruptedException, JSONException { + public void shouldAcceptAnAuthOption() throws InterruptedException, JSONException { final BlockingQueue values = new LinkedBlockingQueue(); IO.Options opts = new IO.Options(); @@ -223,7 +222,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void shouldFireAnErrorEventOnMiddlewareFailure() throws URISyntaxException, InterruptedException, JSONException { + public void shouldFireAnErrorEventOnMiddlewareFailure() throws InterruptedException, JSONException { final BlockingQueue values = new LinkedBlockingQueue(); socket = client("/no"); @@ -245,7 +244,7 @@ public void call(Object... args) { } @Test(timeout = TIMEOUT) - public void shouldThrowOnReservedEvent() throws URISyntaxException, InterruptedException, JSONException { + public void shouldThrowOnReservedEvent() { final BlockingQueue values = new LinkedBlockingQueue(); socket = client("/no"); From 48fec45740092e88c1c72feec7295fb800cd86e3 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 11:06:57 +0200 Subject: [PATCH 06/15] refactor: minor cleanup - replace explicit types by <> - remove unnecessary interface modifiers --- src/main/java/io/socket/client/Ack.java | 2 +- src/main/java/io/socket/client/IO.java | 2 +- src/main/java/io/socket/client/Manager.java | 10 ++-- src/main/java/io/socket/client/On.java | 4 +- src/main/java/io/socket/client/Socket.java | 12 ++-- src/main/java/io/socket/parser/Binary.java | 2 +- src/main/java/io/socket/parser/IOParser.java | 6 +- src/main/java/io/socket/parser/Parser.java | 40 ++++++------- .../java/io/socket/client/Connection.java | 2 +- .../java/io/socket/client/ConnectionTest.java | 58 +++++++++---------- .../io/socket/client/SSLConnectionTest.java | 4 +- .../socket/client/ServerConnectionTest.java | 24 ++++---- .../java/io/socket/client/SocketTest.java | 20 +++---- .../java/io/socket/parser/ByteArrayTest.java | 12 ++-- .../java/io/socket/parser/ParserTest.java | 6 +- src/test/java/io/socket/util/Optional.java | 4 +- 16 files changed, 104 insertions(+), 104 deletions(-) diff --git a/src/main/java/io/socket/client/Ack.java b/src/main/java/io/socket/client/Ack.java index 8bd6a1e8..592838cb 100644 --- a/src/main/java/io/socket/client/Ack.java +++ b/src/main/java/io/socket/client/Ack.java @@ -5,7 +5,7 @@ */ public interface Ack { - public void call(Object... args); + void call(Object... args); } diff --git a/src/main/java/io/socket/client/IO.java b/src/main/java/io/socket/client/IO.java index 0423f27e..1da0c197 100644 --- a/src/main/java/io/socket/client/IO.java +++ b/src/main/java/io/socket/client/IO.java @@ -16,7 +16,7 @@ public class IO { private static final Logger logger = Logger.getLogger(IO.class.getName()); - private static final ConcurrentHashMap managers = new ConcurrentHashMap(); + private static final ConcurrentHashMap managers = new ConcurrentHashMap<>(); /** * Protocol version. diff --git a/src/main/java/io/socket/client/Manager.java b/src/main/java/io/socket/client/Manager.java index 089a2e56..74222bc2 100644 --- a/src/main/java/io/socket/client/Manager.java +++ b/src/main/java/io/socket/client/Manager.java @@ -114,8 +114,8 @@ public Manager(URI uri, Options opts) { opts.callFactory = defaultCallFactory; } this.opts = opts; - this.nsps = new ConcurrentHashMap(); - this.subs = new LinkedList(); + this.nsps = new ConcurrentHashMap<>(); + this.subs = new LinkedList<>(); this.reconnection(opts.reconnection); this.reconnectionAttempts(opts.reconnectionAttempts != 0 ? opts.reconnectionAttempts : Integer.MAX_VALUE); this.reconnectionDelay(opts.reconnectionDelay != 0 ? opts.reconnectionDelay : 1000); @@ -129,7 +129,7 @@ public Manager(URI uri, Options opts) { this.readyState = ReadyState.CLOSED; this.uri = uri; this.encoding = false; - this.packetBuffer = new ArrayList(); + this.packetBuffer = new ArrayList<>(); this.encoder = opts.encoder != null ? opts.encoder : new IOParser.Encoder(); this.decoder = opts.decoder != null ? opts.decoder : new IOParser.Decoder(); } @@ -555,9 +555,9 @@ private void onreconnect() { } - public static interface OpenCallback { + public interface OpenCallback { - public void call(Exception err); + void call(Exception err); } diff --git a/src/main/java/io/socket/client/On.java b/src/main/java/io/socket/client/On.java index b962f131..26b46f34 100644 --- a/src/main/java/io/socket/client/On.java +++ b/src/main/java/io/socket/client/On.java @@ -16,8 +16,8 @@ public void destroy() { }; } - public static interface Handle { + public interface Handle { - public void destroy(); + void destroy(); } } diff --git a/src/main/java/io/socket/client/Socket.java b/src/main/java/io/socket/client/Socket.java index 203c61f4..669732b1 100644 --- a/src/main/java/io/socket/client/Socket.java +++ b/src/main/java/io/socket/client/Socket.java @@ -58,10 +58,10 @@ public class Socket extends Emitter { private String nsp; private Manager io; private Map auth; - private Map acks = new HashMap(); + private Map acks = new HashMap<>(); private Queue subs; - private final Queue> receiveBuffer = new LinkedList>(); - private final Queue> sendBuffer = new LinkedList>(); + private final Queue> receiveBuffer = new LinkedList<>(); + private final Queue> sendBuffer = new LinkedList<>(); public Socket(Manager io, String nsp, Manager.Options opts) { this.io = io; @@ -205,7 +205,7 @@ public void run() { } } - Packet packet = new Packet(Parser.EVENT, jsonArgs); + Packet packet = new Packet<>(Parser.EVENT, jsonArgs); if (ack != null) { logger.fine(String.format("emitting packet with ack id %d", ids)); @@ -302,7 +302,7 @@ private void onpacket(Packet packet) { } private void onevent(Packet packet) { - List args = new ArrayList(Arrays.asList(toArray(packet.data))); + List args = new ArrayList<>(Arrays.asList(toArray(packet.data))); if (logger.isLoggable(Level.FINE)) { logger.fine(String.format("emitting event %s", args)); } @@ -341,7 +341,7 @@ public void run() { jsonArgs.put(arg); } - Packet packet = new Packet(Parser.ACK, jsonArgs); + Packet packet = new Packet<>(Parser.ACK, jsonArgs); packet.id = id; self.packet(packet); } diff --git a/src/main/java/io/socket/parser/Binary.java b/src/main/java/io/socket/parser/Binary.java index b390da62..3ef17116 100644 --- a/src/main/java/io/socket/parser/Binary.java +++ b/src/main/java/io/socket/parser/Binary.java @@ -20,7 +20,7 @@ public class Binary { @SuppressWarnings("unchecked") public static DeconstructedPacket deconstructPacket(Packet packet) { - List buffers = new ArrayList(); + List buffers = new ArrayList<>(); packet.data = _deconstructPacket(packet.data, buffers); packet.attachments = buffers.size(); diff --git a/src/main/java/io/socket/parser/IOParser.java b/src/main/java/io/socket/parser/IOParser.java index 49c1bc8b..b6a959c7 100644 --- a/src/main/java/io/socket/parser/IOParser.java +++ b/src/main/java/io/socket/parser/IOParser.java @@ -122,7 +122,7 @@ private static Packet decodeString(String str) { int i = 0; int length = str.length(); - Packet p = new Packet(Character.getNumericValue(str.charAt(0))); + Packet p = new Packet<>(Character.getNumericValue(str.charAt(0))); if (p.type < 0 || p.type > types.length - 1) { throw new DecodingException("unknown packet type " + p.type); @@ -214,7 +214,7 @@ public void onDecoded (Callback callback) { BinaryReconstructor(Packet packet) { this.reconPack = packet; - this.buffers = new ArrayList(); + this.buffers = new ArrayList<>(); } public Packet takeBinaryData(byte[] binData) { @@ -230,7 +230,7 @@ public Packet takeBinaryData(byte[] binData) { public void finishReconstruction () { this.reconPack = null; - this.buffers = new ArrayList(); + this.buffers = new ArrayList<>(); } } } diff --git a/src/main/java/io/socket/parser/Parser.java b/src/main/java/io/socket/parser/Parser.java index 73635e3c..ea4f6285 100644 --- a/src/main/java/io/socket/parser/Parser.java +++ b/src/main/java/io/socket/parser/Parser.java @@ -5,44 +5,44 @@ public interface Parser { /** * Packet type `connect`. */ - public static final int CONNECT = 0; + int CONNECT = 0; /** * Packet type `disconnect`. */ - public static final int DISCONNECT = 1; + int DISCONNECT = 1; /** * Packet type `event`. */ - public static final int EVENT = 2; + int EVENT = 2; /** * Packet type `ack`. */ - public static final int ACK = 3; + int ACK = 3; /** * Packet type `error`. */ - public static final int CONNECT_ERROR = 4; + int CONNECT_ERROR = 4; /** * Packet type `binary event`. */ - public static final int BINARY_EVENT = 5; + int BINARY_EVENT = 5; /** * Packet type `binary ack`. */ - public static final int BINARY_ACK = 6; + int BINARY_ACK = 6; - public static int protocol = 5; + int protocol = 5; /** * Packet types. */ - public static String[] types = new String[] { + String[] types = new String[] { "CONNECT", "DISCONNECT", "EVENT", @@ -52,29 +52,29 @@ public interface Parser { "BINARY_ACK" }; - public static interface Encoder { + interface Encoder { - public void encode(Packet obj, Callback callback); + void encode(Packet obj, Callback callback); - public interface Callback { + interface Callback { - public void call(Object[] data); + void call(Object[] data); } } - public static interface Decoder { + interface Decoder { - public void add(String obj); + void add(String obj); - public void add(byte[] obj); + void add(byte[] obj); - public void destroy(); + void destroy(); - public void onDecoded(Callback callback); + void onDecoded(Callback callback); - public interface Callback { + interface Callback { - public void call(Packet packet); + void call(Packet packet); } } } diff --git a/src/test/java/io/socket/client/Connection.java b/src/test/java/io/socket/client/Connection.java index 8b97bc22..9f3a533e 100644 --- a/src/test/java/io/socket/client/Connection.java +++ b/src/test/java/io/socket/client/Connection.java @@ -108,7 +108,7 @@ IO.Options createOptions() { } String[] createEnv() { - Map env = new HashMap(System.getenv()); + Map env = new HashMap<>(System.getenv()); env.put("DEBUG", "socket.io:*"); env.put("PORT", String.valueOf(PORT)); String[] _env = new String[env.size()]; diff --git a/src/test/java/io/socket/client/ConnectionTest.java b/src/test/java/io/socket/client/ConnectionTest.java index 13ef7bff..aad9f4c4 100644 --- a/src/test/java/io/socket/client/ConnectionTest.java +++ b/src/test/java/io/socket/client/ConnectionTest.java @@ -30,7 +30,7 @@ public class ConnectionTest extends Connection { @Test(timeout = TIMEOUT) public void connectToLocalhost() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -71,7 +71,7 @@ public void startTwoConnectionsWithSamePathAndDifferentQuerystrings() throws Int @Test(timeout = TIMEOUT) public void workWithAcks() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -112,7 +112,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void receiveDateWithAck() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -137,7 +137,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void sendBinaryAck() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8")); socket = client(); @@ -169,7 +169,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void receiveBinaryDataWithAck() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final byte[] buf = "huehue".getBytes(Charset.forName("UTF-8")); socket = client(); @@ -192,7 +192,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void workWithFalse() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -213,7 +213,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void receiveUTF8MultibyteCharacters() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final String[] correct = new String[] { "てすと", "Я Б Г Д Ж Й", @@ -246,7 +246,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void connectToNamespaceAfterConnectionEstablished() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final Manager manager = new Manager(uri()); socket = manager.socket("/"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -271,7 +271,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void connectToNamespaceAfterConnectionGetsClosed() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final Manager manager = new Manager(uri()); socket = manager.socket("/"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -300,7 +300,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void reconnectByDefault() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.io().on(Manager.EVENT_RECONNECT, new Emitter.Listener() { @Override @@ -321,7 +321,7 @@ public void run() { @Test(timeout = TIMEOUT) public void reconnectManually() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -347,7 +347,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void reconnectAutomaticallyAfterReconnectingManually() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -379,7 +379,7 @@ public void run() { @Test(timeout = 14000) public void attemptReconnectsAfterAFailedReconnect() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.reconnection = true; opts.timeout = 0; @@ -416,7 +416,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void reconnectDelayShouldIncreaseEveryTime() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.reconnection = true; opts.timeout = 0; @@ -466,7 +466,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void notReconnectWhenForceClosed() throws URISyntaxException, InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.timeout = 0; opts.reconnectionDelay = 10; @@ -495,7 +495,7 @@ public void run() { @Test(timeout = TIMEOUT) public void stopReconnectingWhenForceClosed() throws URISyntaxException, InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.timeout = 0; opts.reconnectionDelay = 10; @@ -525,7 +525,7 @@ public void run() { @Test(timeout = TIMEOUT) public void reconnectAfterStoppingReconnection() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.forceNew = true; opts.timeout = 0; @@ -551,7 +551,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void stopReconnectingOnASocketAndKeepToReconnectOnAnother() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final Manager manager = new Manager(uri()); final Socket socket1 = manager.socket("/"); final Socket socket2 = manager.socket("/asd"); @@ -597,7 +597,7 @@ public void run() { @Test(timeout = TIMEOUT) public void connectWhileDisconnectingAnotherSocket() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final Manager manager = new Manager(uri()); final Socket socket1 = manager.socket("/foo"); @@ -624,7 +624,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void tryToReconnectTwiceAndFailWithIncorrectAddress() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = new IO.Options(); opts.reconnection = true; opts.reconnectionAttempts = 2; @@ -656,7 +656,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void tryToReconnectTwiceAndFailWithImmediateTimeout() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = new IO.Options(); opts.reconnection = true; opts.timeout = 0; @@ -689,7 +689,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void notTryToReconnectWithIncorrectPortWhenReconnectionDisabled() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = new IO.Options(); opts.reconnection = false; final Manager manager = new Manager(URI.create("http://localhost:9823"), opts); @@ -723,7 +723,7 @@ public void run() { @Test(timeout = TIMEOUT) public void fireReconnectEventsOnSocket() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); Manager.Options opts = new Manager.Options(); opts.reconnection = true; @@ -758,7 +758,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void fireReconnectingWithAttemptsNumberWhenReconnectingTwice() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); Manager.Options opts = new Manager.Options(); opts.reconnection = true; @@ -793,7 +793,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void emitDateAsString() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -814,7 +814,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void emitDateInObject() throws InterruptedException, JSONException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -844,7 +844,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void sendAndGetBinaryData() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final byte[] buf = "asdfasdf".getBytes(Charset.forName("UTF-8")); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -866,7 +866,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void sendBinaryDataMixedWithJson() throws InterruptedException, JSONException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final byte[] buf = "howdy".getBytes(Charset.forName("UTF-8")); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -899,7 +899,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void sendEventsWithByteArraysInTheCorrectOrder() throws Exception { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final byte[] buf = "abuff1".getBytes(Charset.forName("UTF-8")); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { diff --git a/src/test/java/io/socket/client/SSLConnectionTest.java b/src/test/java/io/socket/client/SSLConnectionTest.java index 2661f0c1..6f475fb3 100644 --- a/src/test/java/io/socket/client/SSLConnectionTest.java +++ b/src/test/java/io/socket/client/SSLConnectionTest.java @@ -89,7 +89,7 @@ public void tearDown() { @Test(timeout = TIMEOUT) public void connect() throws Exception { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.callFactory = sOkHttpClient; opts.webSocketFactory = sOkHttpClient; @@ -113,7 +113,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void defaultSSLContext() throws Exception { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.setDefaultOkHttpWebSocketFactory(sOkHttpClient); IO.setDefaultOkHttpCallFactory(sOkHttpClient); socket = client(); diff --git a/src/test/java/io/socket/client/ServerConnectionTest.java b/src/test/java/io/socket/client/ServerConnectionTest.java index 9360b150..c2e9354e 100644 --- a/src/test/java/io/socket/client/ServerConnectionTest.java +++ b/src/test/java/io/socket/client/ServerConnectionTest.java @@ -26,7 +26,7 @@ public class ServerConnectionTest extends Connection { @Test(timeout = TIMEOUT) public void openAndClose() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -51,7 +51,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void message() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -74,7 +74,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void event() throws Exception { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final JSONObject obj = new JSONObject(); obj.put("foo", 1); @@ -103,7 +103,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void ack() throws Exception { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); final JSONObject obj = new JSONObject(); obj.put("foo", 1); @@ -131,7 +131,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void ackWithoutArgs() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -153,7 +153,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void ackWithoutArgsFromClient() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -188,7 +188,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void closeEngineConnection() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -209,7 +209,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void broadcast() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -242,7 +242,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void room() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @@ -266,7 +266,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void pollingHeaders() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.transports = new String[] {Polling.NAME}; @@ -301,7 +301,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void websocketHandshakeHeaders() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = createOptions(); opts.transports = new String[] {WebSocket.NAME}; @@ -336,7 +336,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void disconnectFromServer() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { diff --git a/src/test/java/io/socket/client/SocketTest.java b/src/test/java/io/socket/client/SocketTest.java index f710f5e2..000fbaa3 100644 --- a/src/test/java/io/socket/client/SocketTest.java +++ b/src/test/java/io/socket/client/SocketTest.java @@ -26,7 +26,7 @@ public class SocketTest extends Connection { @Test(timeout = TIMEOUT) public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketId() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -45,7 +45,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void shouldHaveAnAccessibleSocketIdEqualToServerSideSocketIdOnCustomNamespace() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client("/foo"); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -64,7 +64,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void clearsSocketIdUponDisconnection() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -87,7 +87,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void doesNotFireConnectErrorIfWeForceDisconnectInOpeningState() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = new IO.Options(); opts.timeout = 100; socket = client(opts); @@ -114,7 +114,7 @@ public void run() { @Test(timeout = TIMEOUT) public void shouldChangeSocketIdUponReconnection() throws InterruptedException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client(); socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() { @Override @@ -155,7 +155,7 @@ public void call(Object... objects) { @Test(timeout = TIMEOUT) public void shouldAcceptAQueryStringOnDefaultNamespace() throws InterruptedException, JSONException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client("/?c=d"); socket.emit("getHandshake", new Ack() { @@ -176,7 +176,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void shouldAcceptAQueryString() throws InterruptedException, JSONException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client("/abc?b=c&d=e"); socket.on("handshake", new Emitter.Listener() { @@ -199,7 +199,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void shouldAcceptAnAuthOption() throws InterruptedException, JSONException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); IO.Options opts = new IO.Options(); opts.auth = singletonMap("token", "abcd"); @@ -223,7 +223,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void shouldFireAnErrorEventOnMiddlewareFailure() throws InterruptedException, JSONException { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client("/no"); socket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { @@ -245,7 +245,7 @@ public void call(Object... args) { @Test(timeout = TIMEOUT) public void shouldThrowOnReservedEvent() { - final BlockingQueue values = new LinkedBlockingQueue(); + final BlockingQueue values = new LinkedBlockingQueue<>(); socket = client("/no"); try { diff --git a/src/test/java/io/socket/parser/ByteArrayTest.java b/src/test/java/io/socket/parser/ByteArrayTest.java index a358c15c..9bbb82b5 100644 --- a/src/test/java/io/socket/parser/ByteArrayTest.java +++ b/src/test/java/io/socket/parser/ByteArrayTest.java @@ -20,7 +20,7 @@ public class ByteArrayTest { @Test public void encodeByteArray() { - Packet packet = new Packet(Parser.BINARY_EVENT); + Packet packet = new Packet<>(Parser.BINARY_EVENT); packet.data = "abc".getBytes(Charset.forName("UTF-8")); packet.id = 23; packet.nsp = "/cool"; @@ -29,7 +29,7 @@ public void encodeByteArray() { @Test public void encodeByteArray2() { - Packet packet = new Packet(Parser.BINARY_EVENT); + Packet packet = new Packet<>(Parser.BINARY_EVENT); packet.data = new byte[2]; packet.id = 0; packet.nsp = "/"; @@ -42,7 +42,7 @@ public void encodeByteArrayDeepInJson() throws JSONException { 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 packet = new Packet<>(Parser.BINARY_EVENT); packet.data = data; packet.id = 999; packet.nsp = "/deep"; @@ -54,7 +54,7 @@ public void encodeDeepBinaryJSONWithNullValue() throws JSONException { JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}"); data.put("h", new byte[9]); - Packet packet = new Packet(Parser.BINARY_EVENT); + Packet packet = new Packet<>(Parser.BINARY_EVENT); packet.data = data; packet.nsp = "/"; packet.id = 600; @@ -66,7 +66,7 @@ public void encodeBinaryAckWithByteArray() throws JSONException { JSONArray data = new JSONArray("[a, null, {}]"); data.put(1, "xxx".getBytes(Charset.forName("UTF-8"))); - Packet packet = new Packet(Parser.BINARY_ACK); + Packet packet = new Packet<>(Parser.BINARY_ACK); packet.data = data; packet.id = 127; packet.nsp = "/back"; @@ -79,7 +79,7 @@ public void cleanItselfUpOnClose() { data.put(new byte[2]); data.put(new byte[3]); - Packet packet = new Packet(Parser.BINARY_EVENT); + Packet packet = new Packet<>(Parser.BINARY_EVENT); packet.data = data; packet.id = 0; packet.nsp = "/"; diff --git a/src/test/java/io/socket/parser/ParserTest.java b/src/test/java/io/socket/parser/ParserTest.java index c13005c4..56dc3c26 100644 --- a/src/test/java/io/socket/parser/ParserTest.java +++ b/src/test/java/io/socket/parser/ParserTest.java @@ -27,12 +27,12 @@ public void encodeDisconnection() { @Test public void encodeEvent() throws JSONException { - Packet packet1 = new Packet(Parser.EVENT); + Packet packet1 = new Packet<>(Parser.EVENT); packet1.data = new JSONArray("[\"a\", 1, {}]"); packet1.nsp = "/"; Helpers.test(packet1); - Packet packet2 = new Packet(Parser.EVENT); + Packet packet2 = new Packet<>(Parser.EVENT); packet2.data = new JSONArray("[\"a\", 1, {}]"); packet2.nsp = "/test"; Helpers.test(packet2); @@ -40,7 +40,7 @@ public void encodeEvent() throws JSONException { @Test public void encodeAck() throws JSONException { - Packet packet = new Packet(Parser.ACK); + Packet packet = new Packet<>(Parser.ACK); packet.data = new JSONArray("[\"a\", 1, {}]"); packet.id = 123; packet.nsp = "/"; diff --git a/src/test/java/io/socket/util/Optional.java b/src/test/java/io/socket/util/Optional.java index aadc6036..f4868395 100644 --- a/src/test/java/io/socket/util/Optional.java +++ b/src/test/java/io/socket/util/Optional.java @@ -12,11 +12,11 @@ public static Optional of(T value) { if (value == null) { throw new NullPointerException(); } - return new Optional(value); + return new Optional<>(value); } public static Optional ofNullable(T value) { - return new Optional(value); + return new Optional<>(value); } public static Optional empty() { From 4885e7d59fad78285448694cb5681e8a9ce809ef Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 11:22:38 +0200 Subject: [PATCH 07/15] fix: ensure buffered events are sent in order Before this commit, an event sent in the "connect" handler could be sent before the events that were buffered while disconnected. Related: https://github.com/socketio/socket.io-client/issues/1458 --- src/main/java/io/socket/client/Socket.java | 2 +- .../java/io/socket/client/SocketTest.java | 30 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/socket/client/Socket.java b/src/main/java/io/socket/client/Socket.java index 669732b1..70e35ddd 100644 --- a/src/main/java/io/socket/client/Socket.java +++ b/src/main/java/io/socket/client/Socket.java @@ -367,8 +367,8 @@ private void onack(Packet packet) { private void onconnect(String id) { this.connected = true; this.id = id; - super.emit(EVENT_CONNECT); this.emitBuffered(); + super.emit(EVENT_CONNECT); } private void emitBuffered() { diff --git a/src/test/java/io/socket/client/SocketTest.java b/src/test/java/io/socket/client/SocketTest.java index 000fbaa3..7cc76fd5 100644 --- a/src/test/java/io/socket/client/SocketTest.java +++ b/src/test/java/io/socket/client/SocketTest.java @@ -257,4 +257,34 @@ public void shouldThrowOnReservedEvent() { socket.disconnect(); } + + @Test(timeout = TIMEOUT) + public void shouldEmitEventsInOrder() throws InterruptedException { + final BlockingQueue values = new LinkedBlockingQueue<>(); + + socket = client(); + + socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { + @Override + public void call(Object... objects) { + socket.emit("ack", "second", new Ack() { + @Override + public void call(Object... args) { + values.offer((String) args[0]); + } + }); + } + }); + + socket.emit("ack", "first", new Ack() { + @Override + public void call(Object... args) { + values.offer((String) args[0]); + } + }); + + socket.connect(); + assertThat(values.take(), is("first")); + assertThat(values.take(), is("second")); + } } From e8ffe9d1383736f6a21090ab959a2f4fa5a41284 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Mon, 26 Apr 2021 23:30:53 +0200 Subject: [PATCH 08/15] fix: ensure the payload format is valid This commit should prevent some NPE issues encountered after the parsing of the packet. Related: - https://github.com/socketio/socket.io-client-java/issues/642 - https://github.com/socketio/socket.io-client-java/issues/609 - https://github.com/socketio/socket.io-client-java/issues/505 --- src/main/java/io/socket/client/Manager.java | 28 ++++++------------- src/main/java/io/socket/parser/IOParser.java | 25 +++++++++++++++++ .../java/io/socket/parser/ByteArrayTest.java | 28 +++++++++---------- .../java/io/socket/parser/ParserTest.java | 3 ++ 4 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/main/java/io/socket/client/Manager.java b/src/main/java/io/socket/client/Manager.java index 74222bc2..a3c5f19e 100644 --- a/src/main/java/io/socket/client/Manager.java +++ b/src/main/java/io/socket/client/Manager.java @@ -326,10 +326,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()); } } })); @@ -353,22 +357,6 @@ public void call (Packet packet) { }); } - 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) { this.emit(EVENT_PACKET, packet); } diff --git a/src/main/java/io/socket/parser/IOParser.java b/src/main/java/io/socket/parser/IOParser.java index b6a959c7..e9c53459 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; @@ -183,6 +185,9 @@ private static Packet decodeString(String str) { logger.log(Level.WARNING, "An error occured while retrieving data from JSONTokener", e); throw new DecodingException("invalid payload"); } + if (!isPayloadValid(p.type, p.data)) { + throw new DecodingException("invalid payload"); + } } if (logger.isLoggable(Level.FINE)) { @@ -191,6 +196,26 @@ private static Packet decodeString(String str) { return p; } + private static boolean isPayloadValid(int type, Object payload) { + switch (type) { + case Parser.CONNECT: + case Parser.CONNECT_ERROR: + return payload instanceof JSONObject; + case Parser.DISCONNECT: + return payload == null; + case Parser.EVENT: + case Parser.BINARY_EVENT: + return payload instanceof JSONArray + && ((JSONArray) payload).length() > 0 + && !((JSONArray) payload).isNull(0); + case Parser.ACK: + case Parser.BINARY_ACK: + return payload instanceof JSONArray; + default: + return false; + } + } + @Override public void destroy() { if (this.reconstructor != null) { diff --git a/src/test/java/io/socket/parser/ByteArrayTest.java b/src/test/java/io/socket/parser/ByteArrayTest.java index 9bbb82b5..d48547cf 100644 --- a/src/test/java/io/socket/parser/ByteArrayTest.java +++ b/src/test/java/io/socket/parser/ByteArrayTest.java @@ -1,15 +1,15 @@ package io.socket.parser; -import io.socket.emitter.Emitter; import org.json.JSONArray; import org.json.JSONException; -import org.json.JSONObject; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import static java.util.Arrays.asList; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; @@ -19,9 +19,9 @@ public class ByteArrayTest { private static Parser.Encoder encoder = new IOParser.Encoder(); @Test - public void encodeByteArray() { - Packet packet = new Packet<>(Parser.BINARY_EVENT); - packet.data = "abc".getBytes(Charset.forName("UTF-8")); + public void encodeByteArray() throws JSONException { + Packet packet = new Packet<>(Parser.BINARY_EVENT); + packet.data = new JSONArray(asList("abc", "abc".getBytes(StandardCharsets.UTF_8))); packet.id = 23; packet.nsp = "/cool"; Helpers.testBin(packet); @@ -29,8 +29,8 @@ public void encodeByteArray() { @Test public void encodeByteArray2() { - Packet packet = new Packet<>(Parser.BINARY_EVENT); - packet.data = new byte[2]; + Packet packet = new Packet<>(Parser.BINARY_EVENT); + packet.data = new JSONArray(asList("2", new byte[] { 0, 1 })); packet.id = 0; packet.nsp = "/"; Helpers.testBin(packet); @@ -38,11 +38,11 @@ public void encodeByteArray2() { @Test public void encodeByteArrayDeepInJson() throws JSONException { - JSONObject data = new JSONObject("{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}"); - data.getJSONObject("b").put("why", new byte[3]); - data.getJSONObject("c").getJSONObject("b").put("a", new byte[6]); + JSONArray data = new JSONArray("[{a: \"hi\", b: {}, c: {a: \"bye\", b: {}}}]"); + data.getJSONObject(0).getJSONObject("b").put("why", new byte[3]); + data.getJSONObject(0).getJSONObject("c").getJSONObject("b").put("a", new byte[6]); - Packet packet = new Packet<>(Parser.BINARY_EVENT); + Packet packet = new Packet<>(Parser.BINARY_EVENT); packet.data = data; packet.id = 999; packet.nsp = "/deep"; @@ -51,10 +51,10 @@ public void encodeByteArrayDeepInJson() throws JSONException { @Test public void encodeDeepBinaryJSONWithNullValue() throws JSONException { - JSONObject data = new JSONObject("{a: \"b\", c: 4, e: {g: null}, h: null}"); - data.put("h", new byte[9]); + JSONArray data = new JSONArray("[{a: \"b\", c: 4, e: {g: null}, h: null}]"); + data.getJSONObject(0).put("h", new byte[9]); - Packet packet = new Packet<>(Parser.BINARY_EVENT); + Packet packet = new Packet<>(Parser.BINARY_EVENT); packet.data = data; packet.nsp = "/"; packet.id = 600; diff --git a/src/test/java/io/socket/parser/ParserTest.java b/src/test/java/io/socket/parser/ParserTest.java index 56dc3c26..17d48863 100644 --- a/src/test/java/io/socket/parser/ParserTest.java +++ b/src/test/java/io/socket/parser/ParserTest.java @@ -63,5 +63,8 @@ public void decodeInError() throws JSONException { Helpers.testDecodeError(Parser.EVENT + "2sd"); // event with invalid json data Helpers.testDecodeError(Parser.EVENT + "2[\"a\",1,{asdf}]"); + Helpers.testDecodeError(Parser.EVENT + "2{}"); + Helpers.testDecodeError(Parser.EVENT + "2[]"); + Helpers.testDecodeError(Parser.EVENT + "2[null]"); } } From d324e7f396a444ddd556c3d70a85a28eefb1e02b Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 27 Apr 2021 00:08:16 +0200 Subject: [PATCH 09/15] fix: emit a CONNECT_ERROR event upon connection failure See also: https://github.com/socketio/socket.io-client/commit/53c73749a829b2c98d9a5e45c48f0ae5a22c056c --- src/main/java/io/socket/client/Socket.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/socket/client/Socket.java b/src/main/java/io/socket/client/Socket.java index 70e35ddd..05feff39 100644 --- a/src/main/java/io/socket/client/Socket.java +++ b/src/main/java/io/socket/client/Socket.java @@ -91,7 +91,9 @@ public void call(Object... args) { add(On.on(io, Manager.EVENT_ERROR, new Listener() { @Override public void call(Object... args) { - Socket.super.emit(EVENT_CONNECT_ERROR, args[0]); + if (!Socket.this.connected) { + Socket.super.emit(EVENT_CONNECT_ERROR, args[0]); + } } })); add(On.on(io, Manager.EVENT_CLOSE, new Listener() { From b46da92382a94751b040f5961d523e6b4fa88f92 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 27 Apr 2021 00:30:03 +0200 Subject: [PATCH 10/15] chore(release): prepare release socket.io-client-2.0.1 --- History.md | 11 +++++++++++ pom.xml | 4 ++-- src/site/markdown/installation.md | 4 ++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/History.md b/History.md index a393673e..02f66f43 100644 --- a/History.md +++ b/History.md @@ -1,4 +1,15 @@ +2.0.1 / 2021-04-27 +================== + +### Bug Fixes + +* fix usage with ws:// scheme ([67fd5f3](https://github.com/socketio/socket.io-client-java/commit/67fd5f34a31c63f7884f82ab39386ad343527590)) +* ensure buffered events are sent in order ([4885e7d](https://github.com/socketio/socket.io-client-java/commit/4885e7d59fad78285448694cb5681e8a9ce809ef)) +* ensure the payload format is valid ([e8ffe9d](https://github.com/socketio/socket.io-client-java/commit/e8ffe9d1383736f6a21090ab959a2f4fa5a41284)) +* emit a CONNECT_ERROR event upon connection failure ([d324e7f](https://github.com/socketio/socket.io-client-java/commit/d324e7f396a444ddd556c3d70a85a28eefb1e02b)) + + 2.0.0 / 2020-12-15 ================== diff --git a/pom.xml b/pom.xml index b7f336f0..66b4d2f2 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.socket socket.io-client - 2.0.1-SNAPSHOT + 2.0.1 jar socket.io-client Socket.IO Client Library for Java @@ -30,7 +30,7 @@ https://github.com/socketio/socket.io-client-java scm:git:https://github.com/socketio/socket.io-client-java.git scm:git:https://github.com/socketio/socket.io-client-java.git - HEAD + socket.io-client-2.0.1 diff --git a/src/site/markdown/installation.md b/src/site/markdown/installation.md index 1356a629..1eb05d80 100644 --- a/src/site/markdown/installation.md +++ b/src/site/markdown/installation.md @@ -17,7 +17,7 @@ Add the following dependency to your `pom.xml`. io.socket socket.io-client - 2.0.0 + 2.0.1 ``` @@ -26,7 +26,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:2.0.0') { +compile ('io.socket:socket.io-client:2.0.1') { // excluding org.json which is provided by Android exclude group: 'org.json', module: 'json' } From ad23cfcca6c04dc3a0a0749954c773bb0300bbfb Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 27 Apr 2021 00:35:37 +0200 Subject: [PATCH 11/15] chore(release): prepare for next development iteration --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 66b4d2f2..2d99f665 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 io.socket socket.io-client - 2.0.1 + 2.0.2-SNAPSHOT jar socket.io-client Socket.IO Client Library for Java @@ -30,7 +30,7 @@ https://github.com/socketio/socket.io-client-java scm:git:https://github.com/socketio/socket.io-client-java.git scm:git:https://github.com/socketio/socket.io-client-java.git - socket.io-client-2.0.1 + HEAD From 08bc462ccd4130afc689de198a281bebb20d87b3 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 21 Sep 2021 08:28:30 +0200 Subject: [PATCH 12/15] docs: use implementation instead of compile in gradle (#684) Reference: https://docs.gradle.org/current/userguide/upgrading_version_5.html#dependencies_should_no_longer_be_declared_using_the_compile_and_runtime_configurations --- src/site/markdown/installation.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/site/markdown/installation.md b/src/site/markdown/installation.md index 1eb05d80..9063dbcb 100644 --- a/src/site/markdown/installation.md +++ b/src/site/markdown/installation.md @@ -26,7 +26,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:2.0.1') { +implementation ('io.socket:socket.io-client:2.0.1') { // excluding org.json which is provided by Android exclude group: 'org.json', module: 'json' } From d8d975e5bd059284b79de0c57fc3abb5aac6a7d1 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 21 Sep 2021 08:33:24 +0200 Subject: [PATCH 13/15] docs: update links to the Socket.IO website Some links were broken due to recent updates. --- src/site/markdown/emitting_events.md | 2 +- src/site/markdown/initialization.md | 10 +++++----- src/site/markdown/installation.md | 2 +- src/site/markdown/listening_to_events.md | 2 +- src/site/markdown/migrating_from_1_x.md | 4 ++-- src/site/markdown/socket_instance.md | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/site/markdown/emitting_events.md b/src/site/markdown/emitting_events.md index fce31092..96f32a52 100644 --- a/src/site/markdown/emitting_events.md +++ b/src/site/markdown/emitting_events.md @@ -1,6 +1,6 @@ # Emitting events -See also: https://socket.io/docs/v3/emitting-events/ +See also: https://socket.io/docs/v4/emitting-events/ **Table of content** diff --git a/src/site/markdown/initialization.md b/src/site/markdown/initialization.md index efef638f..72cca0d0 100644 --- a/src/site/markdown/initialization.md +++ b/src/site/markdown/initialization.md @@ -25,7 +25,7 @@ Socket socket = IO.socket("wss://example.com"); // OK, similar to the example ab Socket socket = IO.socket("192.168.0.1:1234"); // NOT OK, missing the scheme part ``` -The path represents the [Namespace](https://socket.io/docs/v3/namespaces/), and not the actual path (see [below](#path)) of the HTTP requests: +The path represents the [Namespace](https://socket.io/docs/v4/namespaces/), and not the actual path (see [below](#path)) of the HTTP requests: ```java Socket socket = IO.socket(URI.create("https://example.com")); // the main namespace @@ -76,7 +76,7 @@ Whether to create a new Manager instance. A Manager instance is in charge of the low-level connection to the server (established with HTTP long-polling or WebSocket). It handles the reconnection logic. -A Socket instance is the interface which is used to sends events to — and receive events from — the server. It belongs to a given [namespace](https://socket.io/docs/v3/namespaces). +A Socket instance is the interface which is used to sends events to — and receive events from — the server. It belongs to a given [namespace](https://socket.io/docs/v4/namespaces). A single Manager can be attached to several Socket instances. @@ -131,7 +131,7 @@ IO.Options options = IO.Options.builder() Socket socket = IO.socket(URI.create("https://example.com"), options); ``` -Note: in that case, sticky sessions are not required on the server side (more information [here](https://socket.io/docs/v3/using-multiple-nodes/)). +Note: in that case, sticky sessions are not required on the server side (more information [here](https://socket.io/docs/v4/using-multiple-nodes/)). #### `upgrade` @@ -177,7 +177,7 @@ IO.Options options = IO.Options.builder() Socket socket = IO.socket(URI.create("https://example.com"), options); ``` -Please note that this is different from the path in the URI, which represents the [Namespace](https://socket.io/docs/v3/namespaces/). +Please note that this is different from the path in the URI, which represents the [Namespace](https://socket.io/docs/v4/namespaces/). Example: @@ -274,7 +274,7 @@ These settings are specific to the given Socket instance. Default value: - -Credentials that are sent when accessing a namespace (see also [here](https://socket.io/docs/v3/middlewares/#Sending-credentials)). +Credentials that are sent when accessing a namespace (see also [here](https://socket.io/docs/v4/middlewares/#sending-credentials)). Example: diff --git a/src/site/markdown/installation.md b/src/site/markdown/installation.md index 9063dbcb..532dc4d3 100644 --- a/src/site/markdown/installation.md +++ b/src/site/markdown/installation.md @@ -3,7 +3,7 @@ | Client version | Socket.IO server | | -------------- | ---------------- | | 0.9.x | 1.x | -| 1.x | 2.x (or 3.1.x / 4.x with [`allowEIO3: true`](https://socket.io/docs/v4/server-initialization/#allowEIO3)) | +| 1.x | 2.x (or 3.1.x / 4.x with [`allowEIO3: true`](https://socket.io/docs/v4/server-options/#alloweio3)) | | 2.x | 3.x / 4.x | ## Installation diff --git a/src/site/markdown/listening_to_events.md b/src/site/markdown/listening_to_events.md index 6caf0a61..6a740658 100644 --- a/src/site/markdown/listening_to_events.md +++ b/src/site/markdown/listening_to_events.md @@ -1,6 +1,6 @@ # Listening to events -See also: https://socket.io/docs/v3/listening-to-events/ +See also: https://socket.io/docs/v4/listening-to-events/ **Table of content** diff --git a/src/site/markdown/migrating_from_1_x.md b/src/site/markdown/migrating_from_1_x.md index f13bebf3..37c56550 100644 --- a/src/site/markdown/migrating_from_1_x.md +++ b/src/site/markdown/migrating_from_1_x.md @@ -7,12 +7,12 @@ Here is the compatibility table: | Java client version | Socket.IO server | | -------------- | ---------------- | | 0.9.x | 1.x | -| 1.x | 2.x (or 3.1.x / 4.x with [`allowEIO3: true`](https://socket.io/docs/v4/server-initialization/#allowEIO3)) | +| 1.x | 2.x (or 3.1.x / 4.x with [`allowEIO3: true`](https://socket.io/docs/v4/server-options/#alloweio3)) | | 2.x | 3.x / 4.x | **Important note:** due to the backward incompatible changes to the Socket.IO protocol, a 2.x Java client will not be able to reach a 2.x server, and vice-versa -Since the Java client matches the Javascript client quite closely, most of the changes listed in the migration guide [here](https://socket.io/docs/v3/migrating-from-2-x-to-3-0) also apply to the Java client: +Since the Java client matches the Javascript client quite closely, most of the changes listed in the migration guide [here](https://socket.io/docs/v4/migrating-from-2-x-to-3-0) also apply to the Java client: - [A middleware error will now emit an Error object](#A_middleware_error_will_now_emit_an_Error_object) - [The Socket `query` option is renamed to `auth`](#The_Socket_query_option_is_renamed_to_auth) diff --git a/src/site/markdown/socket_instance.md b/src/site/markdown/socket_instance.md index 26457164..5a40be45 100644 --- a/src/site/markdown/socket_instance.md +++ b/src/site/markdown/socket_instance.md @@ -113,7 +113,7 @@ socket.on("data", new Emitter.Listener() { ### `Socket.EVENT_CONNECT_ERROR` -This event is fired when the server does not accept the connection (in a [middleware function](https://socket.io/docs/v3/middlewares/#Sending-credentials)). +This event is fired when the server does not accept the connection (in a [middleware function](https://socket.io/docs/v4/middlewares/#sending-credentials)). You need to manually reconnect. You might need to update the credentials: @@ -144,7 +144,7 @@ Here is the list of possible reasons: Reason | Description ------ | ----------- -`io server disconnect` | The server has forcefully disconnected the socket with [socket.disconnect()](https://socket.io/docs/v3/server-api/#socket-disconnect-close) +`io server disconnect` | The server has forcefully disconnected the socket with [socket.disconnect()](https://socket.io/docs/v4/server-api/#socketdisconnectclose) `io client disconnect` | The socket was manually disconnected using `socket.disconnect()` `ping timeout` | The server did not respond in the `pingTimeout` range `transport close` | The connection was closed (example: the user has lost connection, or the network was changed from WiFi to 4G) From d97f4573be978fb8a11f69ceaccc129304799537 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 24 Nov 2021 16:20:57 +0100 Subject: [PATCH 14/15] docs: add example with server to client ack Related: https://github.com/socketio/socket.io-client-java/issues/693 --- src/site/markdown/emitting_events.md | 51 +++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/src/site/markdown/emitting_events.md b/src/site/markdown/emitting_events.md index 96f32a52..5f298ffb 100644 --- a/src/site/markdown/emitting_events.md +++ b/src/site/markdown/emitting_events.md @@ -77,6 +77,27 @@ Events are great, but in some cases you may want a more classic request-response You can add a callback as the last argument of the `emit()`, and this callback will be called once the other side acknowledges the event: +### From client to server + +*Client* + +```java +// Java 7 +socket.emit("update item", 1, new JSONObject(singletonMap("name", "updated")), new Ack() { + @Override + public void call(Object... args) { + JSONObject response = (JSONObject) args[0]; + System.out.println(response.getString("status")); // "ok" + } +}); + +// Java 8 and above +socket.emit("update item", 1, new JSONObject(singletonMap("name", "updated")), (Ack) args -> { + JSONObject response = (JSONObject) args[0]; + System.out.println(response.getString("status")); // "ok" +}); +``` + *Server* ```js @@ -91,15 +112,37 @@ io.on("connection", (socket) => { }); ``` +### From server to client + +*Server* + +```js +io.on("connection", (socket) => { + socket.emit("hello", "please acknowledge", (response) => { + console.log(response); // prints "hi!" + }); +}); +``` + *Client* ```java -socket.emit("update item", 1, new JSONObject(singletonMap("name", "updated")), new Ack() { +// Java 7 +socket.on("hello", new Emitter.Listener() { @Override public void call(Object... args) { - JSONObject response = (JSONObject) args[0]; - System.out.println(response.getString("status")); // "ok" + System.out.println(args[0]); // "please acknowledge" + if (args.length > 1 && args[1] instanceof Ack) { + ((Ack) args[1]).call("hi!"); + } } }); -``` +// Java 8 and above +socket.on("hello", args -> { + System.out.println(args[0]); // "please acknowledge" + if (args.length > 1 && args[1] instanceof Ack) { + ((Ack) args[1]).call("hi!"); + } +}); +``` From 832a6099e8d8235da44f094abeba704870112062 Mon Sep 17 00:00:00 2001 From: Euni <1849581760@qq.com> Date: Sat, 18 Dec 2021 05:38:30 +0800 Subject: [PATCH 15/15] fix: fix emitting of events received during connection establishment (#695) Previously, the event name of packets received during the connection handshake would not be removed from the arguments array: ```java socket.on("my-event", new Emitter.Listener() { @Override public void call(Object... args) { System.out.println(Arrays.toString(args)); // prints ["my-event", "arg1", "arg2", "arg3"] } }); ``` --- src/main/java/io/socket/client/Socket.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/socket/client/Socket.java b/src/main/java/io/socket/client/Socket.java index 05feff39..9e844d94 100644 --- a/src/main/java/io/socket/client/Socket.java +++ b/src/main/java/io/socket/client/Socket.java @@ -376,7 +376,10 @@ private void onconnect(String id) { private void emitBuffered() { List data; while ((data = this.receiveBuffer.poll()) != null) { - String event = (String)data.get(0); + if (data.isEmpty()) { + continue; + } + String event = data.remove(0).toString(); super.emit(event, data.toArray()); } this.receiveBuffer.clear();