Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unity Http listerner

Discussion in 'Multiplayer' started by Olarr, Jan 31, 2024.

  1. Olarr

    Olarr

    Joined:
    Oct 20, 2022
    Posts:
    7
    Hi everyone! I'm posting this here, I don't know if it is the best place.
    I tried to make a simple http server in an Unity app for Android. My goal is to communicate between the app and a web browser. Both devices are on the same local network. I found a script here, and I customised it a very little. My actual script is:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System;
    4. using System.IO;
    5. using System.Net;
    6. using System.Threading;
    7.  
    8.  
    9. //https://gist.github.com/amimaro/10e879ccb54b2cacae4b81abea455b10
    10.  
    11.  
    12. public class UnityHttpListener : MonoBehaviour
    13. {
    14.  
    15.     private HttpListener listener;
    16.     private Thread listenerThread;
    17.  
    18.     void Start()
    19.     {
    20.         listener = new HttpListener();
    21.         //listener.Prefixes.Add("http://localhost:8888/");
    22.         listener.Prefixes.Add("http://*:8080/");
    23.         listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
    24.         listener.Start();
    25.  
    26.         listenerThread = new Thread(startListener);
    27.         listenerThread.Start();
    28.         Debug.Log("Server Started");
    29.     }
    30.  
    31.     private void OnDestroy()
    32.     {
    33.         if (listener != null && listener.IsListening)
    34.         {
    35.             listener.Stop();
    36.             listener.Close();
    37.             listenerThread.Abort();
    38.             Debug.Log("Server Stopped");
    39.         }
    40.     }
    41.  
    42.  
    43.     private void startListener()
    44.     {
    45.         while (true)
    46.         {
    47.             var result = listener.BeginGetContext(ListenerCallback, listener);
    48.             result.AsyncWaitHandle.WaitOne();
    49.         }
    50.     }
    51.  
    52.  
    53.     private void ListenerCallback(IAsyncResult result)
    54.     {
    55.         var context = listener.EndGetContext(result);
    56.  
    57.         Debug.Log("Method: " + context.Request.HttpMethod);
    58.         Debug.Log("LocalUrl: " + context.Request.Url.LocalPath);
    59.  
    60.         if (context.Request.QueryString.AllKeys.Length > 0)
    61.             foreach (var key in context.Request.QueryString.AllKeys)
    62.             {
    63.                 Debug.Log("Key: " + key + ", Value: " + context.Request.QueryString.GetValues(key)[0]);
    64.             }
    65.  
    66.         if (context.Request.HttpMethod == "GET")
    67.         {
    68.             string responseString = GetResponseBasedOnParameters(context.Request.QueryString);
    69.             byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    70.  
    71.             context.Response.ContentLength64 = buffer.Length;
    72.             Stream output = context.Response.OutputStream;
    73.             output.Write(buffer, 0, buffer.Length);
    74.         }
    75.  
    76.  
    77.         else if (context.Request.HttpMethod == "POST")
    78.         {
    79.             Thread.Sleep(1000);
    80.             var data_text = new StreamReader(context.Request.InputStream,
    81.                                              context.Request.ContentEncoding).ReadToEnd();
    82.             Debug.Log($"Message received: {data_text}");
    83.         }
    84.  
    85.  
    86.  
    87.         context.Response.Close();
    88.     }
    89.  
    90.     private string GetResponseBasedOnParameters(System.Collections.Specialized.NameValueCollection parameters)
    91.     {
    92.         string name = parameters["name"];
    93.         string age = parameters["age"];
    94.  
    95.         return "No parameters used but this is just for the test.";
    96.     }
    97. }
    This works well on the same device: app running in the Unity editor, and a web browser on that same PC, or app running on my phone and web browser of this phone. But it never works between PC and phone. I have tried with another PC, same result. I tried to open port 8080, for both incoming and outgoing traffic, in the Windows Firewall, but it don't work. The app has a script which display the local address of the device, so IP addresses are correct. In the web browser, I type 192.168.1.45:8080 (for example - this is the actual local address of my PC). I tried other ports. I tried to shut down editor before doing the test between app on phone and browser on PC.
    Does anyone have an idea to help me? I'm a total beginner in all that network stuff. I did some research before posting here, but have not found anything which can help me.
     
  2. Olarr

    Olarr

    Joined:
    Oct 20, 2022
    Posts:
    7
    OK, I just found the solution. That was my firewall. I shuted it down and it works, I can see the message in the browser of another devices.