WebServer.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using UnityEngine;
  7. public class WebServer : MonoBehaviour
  8. {
  9. private TcpListener listener;
  10. private string rootDirectory;
  11. private const string DefaultFileName = "index.html";
  12. private const string NotFoundFileName = "404.html";
  13. public int port = 8080;
  14. public string directory = "wwwroot";
  15. void Start()
  16. {
  17. rootDirectory = Path.Combine(Application.streamingAssetsPath, directory);
  18. listener = new TcpListener(IPAddress.Any, port);
  19. listener.Start();
  20. Debug.Log("Web server started on port " + port);
  21. }
  22. void Update()
  23. {
  24. if (listener.Pending())
  25. {
  26. TcpClient client = listener.AcceptTcpClient();
  27. HandleRequest(client);
  28. }
  29. }
  30. void HandleRequest(TcpClient client)
  31. {
  32. NetworkStream stream = client.GetStream();
  33. StreamReader reader = new StreamReader(stream);
  34. StreamWriter writer = new StreamWriter(stream);
  35. string request = reader.ReadLine();
  36. Debug.Log("Request: " + request);
  37. string[] tokens = request.Split(' ');
  38. string method = tokens[0];
  39. string url = tokens[1];
  40. if (url == "/")
  41. {
  42. url += DefaultFileName;
  43. }
  44. string filePath = Path.Combine(rootDirectory, url.Substring(1));
  45. if (File.Exists(filePath))
  46. {
  47. string contentType = GetContentType(filePath);
  48. byte[] fileBytes = File.ReadAllBytes(filePath);
  49. writer.Write("HTTP/1.0 200 OK\r\n");
  50. writer.Write("Content-Type: " + contentType + "\r\n");
  51. writer.Write("Content-Length: " + fileBytes.Length + "\r\n");
  52. writer.Write("\r\n");
  53. writer.Flush();
  54. stream.Write(fileBytes, 0, fileBytes.Length);
  55. }
  56. else
  57. {
  58. string notFoundPath = Path.Combine(rootDirectory, NotFoundFileName);
  59. if (File.Exists(notFoundPath))
  60. {
  61. byte[] notFoundBytes = File.ReadAllBytes(notFoundPath);
  62. writer.Write("HTTP/1.0 404 Not Found\r\n");
  63. writer.Write("Content-Type: text/html\r\n");
  64. writer.Write("Content-Length: " + notFoundBytes.Length + "\r\n");
  65. writer.Write("\r\n");
  66. writer.Flush();
  67. stream.Write(notFoundBytes, 0, notFoundBytes.Length);
  68. }
  69. }
  70. writer.Close();
  71. reader.Close();
  72. client.Close();
  73. }
  74. string GetContentType(string filePath)
  75. {
  76. string extension = Path.GetExtension(filePath);
  77. switch (extension)
  78. {
  79. case ".html":
  80. case ".htm":
  81. return "text/html";
  82. case ".css":
  83. return "text/css";
  84. case ".js":
  85. return "text/javascript";
  86. case ".jpg":
  87. case ".jpeg":
  88. return "image/jpeg";
  89. case ".gif":
  90. return "image/gif";
  91. case ".png":
  92. return "image/png";
  93. default:
  94. return "application/octet-stream";
  95. }
  96. }
  97. void OnDestroy()
  98. {
  99. listener.Stop();
  100. }
  101. }