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.

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