private void Start()
{
ws = new WebSocket("ws://localhost:8080");
ws.Connect();
ws.OnMessage += (sender, e) =>
{
var data = e.Data;
Debug.Log($"Packet received: {data}");
object json = JsonConvert.DeserializeObject(data);
Debug.Log("test");
Debug.Log(json);
};
}
This is connecting to my Node.JS websocket server. Whenever I press space, the game sends a test packet to the server, and the server should respond with a packet like {"type":"setPos","pos":{"x":0,"y":0,"z":0}}, and Unity logs that. However, it never logs “test”. As a matter of fact, any code I put past the JsonConvert never runs. I also tried using JsonUtility, but that doesn’t work either. Please help!
And setting up a proxy can be very helpful too, in order to compare traffic:
Problems with Unity “tiny lite” built-in JSON:
In general I highly suggest staying away from Unity’s JSON “tiny lite” package. It’s really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.
Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window → Package Manager).