samedi 25 décembre 2021

Unity - web request only works on localhost

I use Unity webRequest to fetch mjpeg stream from server. But it only works on localhost. If I move the server to another machine in the same LAN. Unity response connection refused.

The script is below. If the url is http://locahost:7777/img it's fine. I've also try http://127.0.0.1:7777/img and http://192.168.0.2:7777/img (localhost IP) all of them work fine. But when I host the server on another machine http://192.168.0.7:7777/img it fail.

var webRequest = WebRequest.Create("http://192.168.0.4:7777/img");
    webRequest.Method = "GET";
    List<byte> frameBuffer = new List<byte>();

    int lastByte = 0x00;
    bool addToBuffer = false;

    BufferedStream buffer = null;
    try
    {
      Stream stream = webRequest.GetResponse().GetResponseStream();
      buffer = new BufferedStream(stream);
      trackedBuffers.Add(buffer);
    }
    catch (System.Exception ex)
    {
      Debug.LogError(ex);
    }

I've use HTML to test and all of them works great.

<html>

<body>
  <img src="http://localhost:7777/img">
  <img src="http://192.168.0.2:7777/img">
  <img src="http://192.168.0.4:7777/img">
  </img>
</body>

</html>



Aucun commentaire:

Enregistrer un commentaire