itisnajim / SocketIOUnity

A Wrapper for socket.io-client-csharp to work with Unity.
MIT License
393 stars 67 forks source link

Socket return value is array #39

Closed wnsvy223 closed 1 year ago

wnsvy223 commented 1 year ago

Thank you for providing us with a useful open source.

First, my development environment.

[node.js server] "socket.io": "^4.2.0"

[Unity] Unity Editor : 2021.3.3f1 SocketIOUnity : 1.1.4

When I received the socket event, I expected the result value to be received in json as follows. {"room" : {"hostIpAddress" : "xxx.x.x.x", "maxPlayer" : "3", "currentPlayer" : "M-A2-UtAAzhZYS93AAAB"}}

But the actual result is wrapped in an array [{"room" : {"hostIpAddress" : "xxx.x.x.x", "maxPlayer":"3", "currentPlayer" : "M-A2-UtAAzhZYS93AAAB"}}]

Is this a bug? If I'm using it wrong, how can I fix it?

Thanks.

itisnajim commented 1 year ago

can i see the emit code? are you using Newtonsoft.Json for serialization ? you have to create a class model to instantiate an object from, see TestClass & TestClass2: https://github.com/itisnajim/SocketIOUnity/blob/main/Samples~/Sample/SocketManager.cs

wnsvy223 commented 1 year ago

Oh, I see.

As you said, it works fine using the custom class, as follows ClassA clazz = response.GetValue<ClassA>();

The reason why I went through this situation is, When I used the previous version, I used it for debugging purposes as follows.

[server]

io.emit("event", { test: "test"});

previous version [1.1.2~1.1.3]

socket.OnUnityThread("event", (response) =>
{
    Debug.Log("value :" + response);  // [{"test" : "test"}]
    Debug.Log("value :" + response.GetValue().ToString());  // {"test" : "test"}
    ClassA clazz = JsonConvert.DeserializeObject<ClassA>(response.GetValue().ToString());
});

current version [1.1.4]

socket.OnUnityThread("event", (response) =>
{
    Debug.Log("value :" + response.ToString());  // [{"test" : "test"}]
    Debug.Log("value :" + response.GetValue().ToString());  // syntax error red line. So only using custom class syntax could be used.
});

Thank you sir.

itisnajim commented 1 year ago

you can do like in the Readme https://github.com/itisnajim/SocketIOUnity#receiving

var obj = response.GetValue<SomeClass>();