From ad23cfcca6c04dc3a0a0749954c773bb0300bbfb Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Tue, 27 Apr 2021 00:35:37 +0200 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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();