socket.io-client-java icon indicating copy to clipboard operation
socket.io-client-java copied to clipboard

socket.io client sometimes sends arrays wrong

Open supermaximus80 opened this issue 2 years ago • 5 comments

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.

supermaximus80 avatar May 21 '23 22:05 supermaximus80

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

shahabrar7746 avatar Dec 24 '23 21:12 shahabrar7746

@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?

supermaximus80 avatar Dec 25 '23 22:12 supermaximus80

its not a bug. its like a feacture of java

shahabrar7746 avatar Jan 03 '24 19:01 shahabrar7746

@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.

supermaximus80 avatar Jan 04 '24 22:01 supermaximus80

The emit arguments are added in a JSONArray here: https://github.com/socketio/socket.io-client-java/blob/ad3a930e346ad54acd8e895b01418f7b936776dd/src/main/java/io/socket/client/Socket.java#L205-L212

Which are then stringified here: https://github.com/socketio/socket.io-client-java/blob/ad3a930e346ad54acd8e895b01418f7b936776dd/src/main/java/io/socket/parser/IOParser.java#L60-L62

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.

darrachequesne avatar Jan 05 '24 10:01 darrachequesne