Received wrong message from socket
I put java WebSocket as my game server, using JsonUtility.ToJson to make Vector2 object become JSON String and let send back, But something goes wrong when it's sent back. Here's my code to upload the data(seems no problem, because the data I received from the server is correct)
private void Send(ClientWebSocket client, CancellationToken ct, string message)
{
// Debug.Log("send" + message);
client.SendAsync(new ArraySegment<byte>(Encoding.UTF8.GetBytes(message)), WebSocketMessageType.Text, true, ct);
}
public void PlayerMovement(ClientWebSocket client, CancellationToken ct, Vector2 angle, Vector2 distance)
{
packet = new Packet();
PlayerPosition po = new PlayerPosition(angle, distance);
packet.position = po;
Send(client, ct, JsonUtility.ToJson(packet));
}
But when I receive the data from the server, it goes wrong. Here's my code to receive:
await ws.ReceiveAsync(new ArraySegment<byte>(result), new CancellationToken());
var str = Encoding.UTF8.GetString(result,0,result.Length);
Debug.Log("received2 " + str);
The data I send from the server is like this:
{"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-2.0,"y":0.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-4.0,"y":2.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-2.0,"y":1.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-2.0,"y":0.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-3.0,"y":0.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-2.0,"y":-1.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-1.0,"y":0.0}}} {"position":{"angle":{"x":0.0,"y":1.0},"position":{"x":-1.0,"y":0.0}}}
The data I received at the unity c# script
Obviously, it's not the correct JSON format, but I don't know what's going on. How can I fix this problem?