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!