What I'm trying to Do is getting a Json with .Net.WebSockets and parse it with Simple Json, however I face the error "Json format seems invalid".
at first i suspected json that is coming from server might be invalid but after some investigation i tried to parse the exact json from file not from server and it was ok , so i think there must be something wrong in my code but i can't seem to find it ( I'm new to websockets ).
public async void StartMatch()
{
await ConnectToServer();
await SendMassage();
FoundPlayer(await GetMassage());
}
public void FoundPlayer(string oppInfo)
{
Debug.Log(oppInfo);
JSONNode node = JSON.Parse(oppInfo);
}
public async Task SendMassage()
{
JSONNode node = JSON.Parse(json);
node["data"]["categoryId"].AsInt = readyToPlay.id;
string id = node.ToString();
var encoded = Encoding.UTF8.GetBytes(id);
var buffer = new ArraySegment<byte>(encoded, 0, encoded.Length);
await Task.Run(() => ws.SendAsync(buffer, WebSocketMessageType.Text, true, CancellationToken.None));
Debug.Log("Sent");
}
public async Task<string> GetMassage()
{
byte[] bufferSize = new byte[5000];
var buffer = new ArraySegment<byte>(bufferSize, 0, bufferSize.Length);
var results = await Task.Run(() => ws.ReceiveAsync(buffer, CancellationToken.None));
string bufferResult = Encoding.Default.GetString(buffer.Array);
wait.SetActive(false);
Debug.Log("Received");
return bufferResult;
}
and here is Json if you need it.
{
"action":"MatchStart",
"message":{
"opponentInfo":{
"firstName":"James",
"lastName":"Newell",
"username":"JN",
"image":null,
"level":0,
"cityName":null,
"provinceName":null
},
"question":{
"id":142570,
"text":"کمپانی «گوگل» متعلق به کدام کشور میباشد؟",
"image":"",
"audio":"",
"options":[
{
"id":715538,
"option":"ایتالیا"
},
{
"id":715536,
"option":"آمریکا"
},
{
"id":715539,
"option":"بریتانیا"
},
{
"id":715537,
"option":"آلمان"
}
]
},
"questionIndex":1,
"questionsCount":7,
"gameToken":"9d545d801bffe656a9140a877b050210",
"validTime":10,
"userId":75714
}
}