You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.3 KiB

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using UnityEngine;
  5. using System.Net;
  6. using System.Text;
  7. using System.IO;
  8. public class BrowserServer : MonoBehaviour
  9. {
  10. [SerializeField]
  11. [Tooltip("Local Path in Streaming Assets")]
  12. private string LocalPath;
  13. [SerializeField]
  14. [Tooltip("Browsers by default use 80")]
  15. private int port = 80;
  16. SimpleHTTPServer server;
  17. // Start is called before the first frame update
  18. void Start()
  19. {
  20. server = new SimpleHTTPServer(Path.Combine(Application.streamingAssetsPath,LocalPath),port);
  21. }
  22. private void OnDisable()
  23. {
  24. server.Stop();
  25. }
  26. private void StartServer()
  27. {
  28. if (!HttpListener.IsSupported)
  29. {
  30. Debug.LogWarning("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
  31. return;
  32. }
  33. string responseString = "<html><body><h1>Error Connecting to server</h1></body></html>";
  34. // Create a listener.
  35. HttpListener server = new HttpListener();
  36. // Add the prefixes.
  37. server.Prefixes.Add("http://*:" + port.ToString() + "/");
  38. server.Start();
  39. Debug.Log(" HTML Listening...");
  40. // while (enabled)
  41. // {
  42. HttpListenerContext context = server.GetContext();
  43. HttpListenerResponse response = context.Response;
  44. string page = Path.Combine(Application.streamingAssetsPath, LocalPath, context.Request.Url.LocalPath);
  45. Debug.Log("Client asked for: " + context.Request.Url.LocalPath);
  46. if (page.EndsWith("/"))
  47. page += "index.html";
  48. Debug.Log("Client asked for: " + page);
  49. if (page == string.Empty)
  50. page = "index.html";
  51. try
  52. {
  53. TextReader tr = new StreamReader(page);
  54. string msg = tr.ReadToEnd();
  55. byte[] buffer = Encoding.UTF8.GetBytes(msg);
  56. response.ContentLength64 = buffer.Length;
  57. Stream st = response.OutputStream;
  58. st.Write(buffer, 0, buffer.Length);
  59. }
  60. catch (IOException e)
  61. {
  62. Debug.LogError(e);
  63. }
  64. context.Response.Close();
  65. //}
  66. server.Stop();
  67. Task.Run(() => StartServer());
  68. }
  69. private void Respond(HttpListenerContext context)
  70. {
  71. string responseString = "<html><body><h1>Error Connecting to server</h1></body></html>";
  72. try
  73. {
  74. responseString = System.IO.File.ReadAllText(Path.Combine(Application.streamingAssetsPath, LocalPath, context.Request.Url.LocalPath));
  75. Debug.Log(responseString);
  76. }
  77. catch (System.IO.IOException e)
  78. {
  79. Debug.LogError(e);
  80. }
  81. HttpListenerRequest request = context.Request;
  82. // Obtain a response object.
  83. HttpListenerResponse response = context.Response;
  84. // Construct a response.
  85. byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
  86. // Get a response stream and write the response to it.
  87. response.ContentLength64 = buffer.Length;
  88. System.IO.Stream output = response.OutputStream;
  89. output.Write(buffer, 0, buffer.Length);
  90. // You must close the output stream.
  91. output.Close();
  92. }
  93. }