Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.io client sometimes sends arrays wrong #743

Open
supermaximus80 opened this issue May 21, 2023 · 5 comments
Open

socket.io client sometimes sends arrays wrong #743

supermaximus80 opened this issue May 21, 2023 · 5 comments
Labels

Comments

@supermaximus80
Copy link

Describe the bug
Sometimes socket.io client on android sends array of int[] wront. On server I receive something which looks like this:
SELL_COMPANIES_SELECTED: {"sellCIndices":"[I@50bf616","nexEvent":"PROCESS_GAME_FIELD"}
As you can see sellCIndices":"[I@50bf616" which is something I can't undertand.
Normally received data should look like this
SELL_COMPANIES_SELECTED: {"nexEvent":"PROCESS_GAME_FIELD","sellCIndices":[0]}

To Reproduce

Socket.IO server version: 4.6.1

Server

socket.on('SELL_COMPANIES_SELECTED', function(data) {
        console.log("SELL_COMPANIES_SELECTED: %s", JSON.stringify(data));
        console.log("sellCIndices: " + data.sellCIndices);
        console.log("nexEvent: %s", data.nexEvent);
        socket.to(roomId).emit('SELL_COMPANIES_SELECTED', data);
});
  // ...

Socket.IO java client version: 2.1.0

Client

int[] sellCIndices = objsToIndices(currentPlayer.getOwnedProperty(), sellingCompanies);
JSONObject obj = new JSONObject();
try {
    obj.put("sellCIndices", sellCIndices);
    obj.put("nexEvent", nextEvent);
 } catch (JSONException e) {
    Gdx.app.error(STAG, e.toString());
}
socket.emit(event.toString(), obj);
Gdx.app.debug(STAG, String.format("sending SELL_COMPANIES_SELECTED: %s, next event = %s", Arrays.toString(sellCIndices), nextEvent));

Expected behavior
On my server I expect to see something like "sellCIndices":[0]. But, sometimes I see strange data like "sellCIndices":"[I@50bf616"

Platform:

  • Samsung S7, Motorola edge X30
  • OS: Android 8, Android 12

Additional context
This bug breaks all my application logic, it's very important to fix it or to find a workaround.

@shahabrar7746
Copy link

looks like you are sending hash value of your 'sellCIndices' array. instead you can send content of your array using "Arrays.toString()" method.
just replace " obj.put("sellCIndices", sellCIndices);
"
with " obj.put("sellCIndices", Arrays.toString(sellCIndices));
". from client code

@supermaximus80
Copy link
Author

@shahabrar7746 Hi. In this case why this happens very seldom and most of the time array sends correctly?
An example from the official documentation:

byte[] buffer = "abc".getBytes(StandardCharsets.UTF_8);
JSONObject object = new JSONObject();
object.put("test", "42");

socket.emit("hello", 1, "2", buffer, object);

So my question is, is this the bug? Should we always use Arrays.toString() while sending arrays?

@shahabrar7746
Copy link

its not a bug. its like a feacture of java

@supermaximus80
Copy link
Author

supermaximus80 commented Jan 4, 2024

@shahabrar7746 Why this feature sometimes works, sometimes doesn't? It's the bug, but the problem is, is it server parser bug or java client.

@darrachequesne
Copy link
Member

The emit arguments are added in a JSONArray here:

JSONArray jsonArgs = new JSONArray();
jsonArgs.put(event);
if (args != null) {
for (Object arg : args) {
jsonArgs.put(arg);
}
}

Which are then stringified here:

if (obj.data != null) {
str.append(obj.data);
}

It seems that the Java arrays like new int[] { 1, 2, 3 } are sometimes not properly converted to [1,2,3] on some platforms.

As a temporary workaround, I think converting the arrays to a list with Arrays.asList() should fix the issue. Could you please check?

In the meantime, we will try to fix it in the codebase directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants