2

To test the networking part of my Unity application, I have an "imitatee" game object that moves according to the keyboard input. The networking manager component (1) gets the movement data of the "imitatee" and sends it to a node.js localhost server using PUT and also (2) gets back the from localhost and applies it to the "imitator" object. The program worked fine, but after a while I got the error:

IOException: Too many open files

A window also popped up in the Unity editor saying that "Opening files failed" and then the editor crashed. I think that it was caused by memory not being disposed properly but have not figured out exactly how. Here is the GameManager code:

NetworkingManager.cs

public class Data
{
    public float posX;
    ...

    public Data(float posX...)
    {
        this.posX = posX;
        ...
    }
}

public class NetworkingManager : MonoBehaviour
{

    private GameObject imitator;
    private GameObject imitatee;

    private Data data;
    private string jsonData;
    private Data receivedData;

    private string uri = "http://localhost:3000/";

    private void Awake()
    {
        // Assign references
        ...
    }

    private IEnumerator SendData()
    {
        data = new Data(
            imitatee.transform.position.x,
            ...
            );

        jsonData = JsonUtility.ToJson(data);

        using (UnityWebRequest req = UnityWebRequest.Put(uri, jsonData))
        {
            req.SetRequestHeader("Content-Type", "application/json");
            yield return req.SendWebRequest();
    

            if (req.isNetworkError || req.isHttpError)
            {
                Debug.Log(req.error);
            }
        }

    }

    private IEnumerator ReceiveData()
    {

        using (UnityWebRequest req = UnityWebRequest.Get(uri))
        {
            yield return req.SendWebRequest();

            if (req.isNetworkError)
            {
                Debug.Log(req.error);
            }
            else
            {
                receivedData = JsonUtility.FromJson<Data>(req.downloadHandler.text);

                imitator.transform.position = new Vector3(
                    receivedData.posX,
                    ...
                    );

                ...
            }
        }
        
    }

    void Update()
    {
        StartCoroutine(SendData());
        StartCoroutine(ReceiveData());
    }
}

Besides, there was no error logged for the node.js backend.

backend.js

const express = require('express')
const app = express()
app.use(express.json())
const port = 3000
var data

app.get('/', (req, res) => {
  if (data) res.json(data)
})

app.put('/', (req, res) => {
  data = req.body
  console.log("Received " + req.body) 
})

app.listen(port, () => {
  console.log(`App listening at http://localhost:${port}`)
})

Appreciate your help!

Xi Liu
  • 559
  • 9
  • 18
  • Don't use coroutines as they teach bad habits from the perspective of a c# developer and will most likely lead to a lynching in a regular c# job –  Jan 15 '22 at 03:06
  • In any event, did you mean to start your coroutine from `Start` instead of `Update`? Not even _Battlefield 4_ sends updates in excess of 60Hz let alone logarithmically –  Jan 15 '22 at 03:07
  • Hi MickyD thanks for your reply! Yes I tried putting the coroutines in ```Start``` and wrapping the code inside with ```while(true)```, but for some reason ```SendData()``` was only called once even though ```ReceiveData()``` was continuously called – Xi Liu Jan 15 '22 at 03:11
  • Just call each method explicitly during `Update()` after a certain time period has elapsed and do away with hacky coroutines entirely. Less confusing, easier to debug, no lynching –  Jan 15 '22 at 03:13
  • Yeah I also tried this method. This pushed back the time at which the crash happened, which suggested that there was likely some memory problem accumulating as ```SendData()``` and ```ReceiveData()``` executed each time – Xi Liu Jan 15 '22 at 03:18
  • @MickyD if not Coroutines what would you use then? `they teach bad habits from the perspective of a c# developer` .. being what exactly for example? I think as a developer you should be able enough to adopt to whatever your framework offers and Coroutines is a pretty good tool in Unity that is often way better to maintain and more efficient than other alternatives ... I agree of course that in this specific case starting a routine each frame makes little sense ... @OP why even do web requests? They have quite an overhead.. why not direct TCP/UDP transfer? – derHugo Jan 15 '22 at 09:59

0 Answers0