Search Unity

Question HTTP Server to render images on HoloLens doesn't work

Discussion in 'Editor & General Support' started by Michael_Sluckin, Mar 1, 2021.

  1. Michael_Sluckin

    Michael_Sluckin

    Joined:
    Dec 7, 2020
    Posts:
    8
    Hi,

    I programmed an Unity Server what receives Images via HTTP and renders the received images in an spirit object.

    This Programm works in the Unity emulator.

    When I debug, the debugger stops at the the "listener.Start();" line.

    Does anybody know what the problem is?

    Code (CSharp):
    1. using UnityEngine.SceneManagement;
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using System.Drawing;
    7. using System.Net;
    8. using System.Threading;
    9. using System.Threading.Tasks;
    10. using UnityEngine;
    11. using System.Text.RegularExpressions;
    12. using System.Net.Sockets;
    13. using UnityEngine.UI;
    14. using Color = UnityEngine.Color;
    15.  
    16.  
    17. #if UNITY_EDITOR
    18. #else
    19. using Windows.Networking.Sockets;
    20. using Windows.Networking;
    21. using Windows.Storage;
    22. using Windows.Storage.Streams;
    23. #endif
    24.  
    25.  
    26. public class HTTPServer : MonoBehaviour
    27. {
    28.     private Sprite img1;
    29.     public SpriteRenderer sr;
    30.  
    31.     public void startReceiving()
    32.     {
    33.         ReceiveHTTP();
    34.     }
    35.  
    36.         private async void ReceiveHTTP()
    37.     {
    38.         Texture2D tex = new Texture2D(2, 2);
    39.         var listener = new HttpListener();                                  
    40.         listener.Prefixes.Add("http://+:8087/VDS_png/");              
    41.         listener.Start();                                                  
    42.  
    43.         try
    44.         {
    45.  
    46.  
    47.             while (true)
    48.             {
    49.                 var ctx = await listener.GetContextAsync();              
    50.                 var req = ctx.Request;
    51.                 var stream = req.InputStream;
    52.                 var buf = new byte[20000];
    53.                 int len;
    54.                 while ((len = stream.Read(buf, 0, buf.Length)) != 0)              
    55.                 ctx.Response.StatusCode = 200;                                
    56.                 ctx.Response.Close();
    57.  
    58.                 bool v = tex.LoadImage(buf);
    59.                 if (v)
    60.                 {
    61.                     img1 = Sprite.Create(tex, new Rect(0.0f, 0.0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 100.0f);
    62.                     sr.sprite = img1;
    63.                 }
    64.  
    65.             }
    66.  
    67.         }
    68.         finally
    69.         {
    70.             listener.Stop();
    71.             listener.Close();
    72.         }
    73.  
    74.     }
    75.  
    76. }
    77.