- Home /
Read from node js via c# with Unity
I am programming an application for the hololens (Platform=>UWP) with Unity3D.
Currenty, my NodeJs Server writes one value (that is updating all the time) in a file IP-ADRESS/value.txt
And on my C# Script, I do the following to read that value from the file:
IEnumerator UpdateView() {
UnityWebRequest www = UnityWebRequest.Get("http://192.168.15.5:7777/value.txt");
yield return www.SendWebRequest();
if (www.responseCode == 200) {
// www.downloadHandler.text <- my value
// my code ...
}
}
void Update() {
StartCoroutine(UpdateView());
}
I know, it isn't very effective, because writing and reading a file costs much time, so I want to implement a c# client that can get the values directly from the nodejs script.
I've found this one on the internet, but couldn't make it work. https://www.infoworld.com/article/2917047/microsoft-net/developing-applications-with-nodejs-and-c.html It says TcpClient not found and the using namespace is missing.
Can you give me some advices, how I can solve such a problem?
Thank you very very much
The fact that it's a NodeJS server doesn't really factor into this, but it does let us provide specific advice so ty. Your current solution reads a text file from an http request, which I imagine Unity has streamlined quite a bit. If that's the raw text file, then your response body will just be a string version of that value. What is the hangup you're having?
Alternatively, it sounds like what you actually want to do is interface with some sort of API, where your server sends back specific data. I don't know how your Node server is set up, but I will assume you're using the http server module. If that's the case, then you can set up an alternative response for POST requests to a given relative path that will just return a json object with your current value.
So, you might just have any POST request with an Content-Type: application/json
to "/status" return {"value": "[CurrentStatus]"}