- Home /
websocket and simplejson parsing problem
Hello everybody.
I have the problem, that my while (loop) dosnt really work. Its a modified example from the free websocket asset. I can send json to my node.js server, but i can't receive the json. Obviously there must be something wrong with loop or with receiving of the string. Because my Debug.Log only plays once (only the first "while"). If I do an if reply == null, then I will receive a log again. So it seems there is no data arriving... But shouldn't my Loop working through and through, just only playing once?
Maybe you can help me out of my dilemma. Thank you very much! :)
Edit: The error messages are collapsed... I didn't saw this ;-). So looping works, but there is no information receiving...
using UnityEngine;
using System.Collections;
using System;
using SimpleJSON;
public class EchoTest : MonoBehaviour {
// Use this for initialization
IEnumerator Start () {
WebSocket w = new WebSocket(new Uri("ws://localhost:8000/2"));
yield return StartCoroutine(w.Connect());
var N = new JSONClass();
String P;
N.Add("id", "unityclient");
N.Add("to", "htmlclient");
P = N.ToString();
w.SendString(P);
int i=0;
while (true)
{
Debug.Log("while");
string reply = w.RecvString();
if (reply != null){
Debug.Log("while2");
var rep = JSON.Parse(reply);
Debug.Log("jo");
string jdata = rep["data"];
Debug.Log(jdata);
i++;
}
/*
if (reply!="red"){
Debug.Log ("Received: "+reply);
}
if (reply=="red"){
var replydata = JSONNode.Parse(reply);
var userid = replydata["data"]["userid"];
var message = replydata["data"]["message"];
Debug.Log(userid+":"+message);
}
if (reply == "red")
{
Debug.Log ("Received: "+reply);
w.SendString("red back");
}*/
if (w.error != null)
{
Debug.LogError ("Error: "+w.error);
break;
}
yield return 0;
}
w.Close();
}
}
Your answer

Follow this Question
Related Questions
Since switching to using offline & online scenes in the NetworkManager, Clients will not sync (UNET) 0 Answers
Why I am getting JSON error during the query of Internet Time? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Connect to non-unity server with LLAPI 0 Answers