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.

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