WebServer.cs 6.0 KB

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