WebServer.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Threading;
  6. using UnityEngine;
  7. namespace Unity3D2Babylon
  8. {
  9. public static class WebServer
  10. {
  11. private static readonly HttpListener listener;
  12. const string HtmlResponseText = @"
  13. <!doctype html>
  14. <html>
  15. <head>
  16. <title>Babylon.js</title>
  17. <script type='text/javascript' src='http://www.babylonjs.com/oimo.js'></script>
  18. <script type='text/javascript' src='http://www.babylonjs.com/cannon.js'></script>
  19. <script type='text/javascript' src='http://www.babylonjs.com/babylon.js'></script>
  20. <style type='text/css'>
  21. html, body, div, canvas {
  22. width: 100%;
  23. height: 100%;
  24. padding: 0;
  25. margin: 0;
  26. overflow: hidden;
  27. }
  28. #debugLayerButton {
  29. position: absolute;
  30. border: white solid 1px;
  31. background: rgba(128, 128, 128, 0.3);
  32. color: white;
  33. left: 50%;
  34. width: 100px;
  35. margin-left:-50px;
  36. bottom: 10px;
  37. }
  38. </style>
  39. </head>
  40. <body>
  41. <canvas id='canvas'></canvas>
  42. <button id='debugLayerButton'>Debug layer</button>
  43. <script type='text/javascript'>
  44. var canvas = document.getElementById('canvas');
  45. var engine = new BABYLON.Engine(canvas, true);
  46. BABYLON.SceneLoader.Load('', '###SCENE###', engine, function (newScene) {
  47. newScene.activeCamera.attachControl(canvas);
  48. engine.runRenderLoop(function() {
  49. newScene.render();
  50. });
  51. window.addEventListener('resize', function () {
  52. engine.resize();
  53. });
  54. document.getElementById('debugLayerButton').addEventListener('click', function () {
  55. if (newScene.debugLayer.isVisible()) {
  56. newScene.debugLayer.hide();
  57. } else {
  58. newScene.debugLayer.show();
  59. }
  60. });
  61. });
  62. </script>
  63. </body>
  64. </html>";
  65. public const int Port = 45478;
  66. public static bool IsSupported { get; private set; }
  67. static WebServer()
  68. {
  69. try
  70. {
  71. listener = new HttpListener();
  72. if (!HttpListener.IsSupported)
  73. {
  74. IsSupported = false;
  75. return;
  76. }
  77. listener.Prefixes.Add("http://localhost:" + Port + "/");
  78. listener.Start();
  79. ThreadPool.QueueUserWorkItem(Listen);
  80. IsSupported = true;
  81. }
  82. catch
  83. {
  84. IsSupported = false;
  85. }
  86. }
  87. public static string SceneFilename { get; set; }
  88. public static string SceneFolder { get; set; }
  89. static System.Random r = new System.Random();
  90. static void Listen(object state)
  91. {
  92. try
  93. {
  94. while (listener.IsListening)
  95. {
  96. var context = listener.GetContext();
  97. var request = context.Request;
  98. var url = request.Url;
  99. context.Response.AddHeader("Cache-Control", "no-cache");
  100. if (string.IsNullOrEmpty(url.LocalPath) || url.LocalPath == "/")
  101. {
  102. var responseText = HtmlResponseText.Replace("###SCENE###", SceneFilename+"?once="+r.Next());
  103. WriteResponse(context, responseText);
  104. }
  105. else
  106. {
  107. try
  108. {
  109. var path = Path.Combine(SceneFolder, WWW.UnEscapeURL(url.PathAndQuery.Substring(1)));
  110. var questionMarkIndex = path.IndexOf("?");
  111. if (questionMarkIndex != -1)
  112. {
  113. path = path.Substring(0, questionMarkIndex);
  114. }
  115. var hashIndex = path.IndexOf("#");
  116. if (hashIndex != -1)
  117. {
  118. path = path.Substring(0, hashIndex);
  119. }
  120. var buffer = File.ReadAllBytes(path);
  121. WriteResponse(context, buffer);
  122. }
  123. catch
  124. {
  125. context.Response.StatusCode = 404;
  126. context.Response.Close();
  127. }
  128. }
  129. }
  130. }
  131. catch
  132. {
  133. }
  134. }
  135. static void WriteResponse(HttpListenerContext context, string s)
  136. {
  137. WriteResponse(context.Response, s);
  138. }
  139. static void WriteResponse(HttpListenerContext context, byte[] buffer)
  140. {
  141. WriteResponse(context.Response, buffer);
  142. }
  143. static void WriteResponse(HttpListenerResponse response, string s)
  144. {
  145. byte[] buffer = Encoding.UTF8.GetBytes(s);
  146. WriteResponse(response, buffer);
  147. }
  148. static void WriteResponse(HttpListenerResponse response, byte[] buffer)
  149. {
  150. response.ContentLength64 = buffer.Length;
  151. Stream output = response.OutputStream;
  152. output.Write(buffer, 0, buffer.Length);
  153. output.Close();
  154. }
  155. }
  156. }