WebServer.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. engine.runRenderLoop(function() {
  51. newScene.render();
  52. });
  53. window.addEventListener('resize', function () {
  54. engine.resize();
  55. });
  56. document.getElementById('debugLayerButton').addEventListener('click', function () {
  57. if (newScene.debugLayer.isVisible()) {
  58. newScene.debugLayer.hide();
  59. } else {
  60. newScene.debugLayer.show();
  61. }
  62. });
  63. });
  64. </script>
  65. </body>
  66. </html>";
  67. public const int Port = 45478;
  68. public static bool IsSupported { get; private set; }
  69. static WebServer()
  70. {
  71. try
  72. {
  73. listener = new HttpListener();
  74. if (!HttpListener.IsSupported)
  75. {
  76. IsSupported = false;
  77. return;
  78. }
  79. listener.Prefixes.Add("http://localhost:" + Port + "/");
  80. listener.Start();
  81. runningTask = Task.Run(() => Listen());
  82. IsSupported = true;
  83. }
  84. catch
  85. {
  86. IsSupported = false;
  87. }
  88. }
  89. public static string SceneFilename { get; set; }
  90. public static string SceneFolder { get; set; }
  91. static Random r = new Random();
  92. static void Listen()
  93. {
  94. try
  95. {
  96. while (listener.IsListening)
  97. {
  98. var context = listener.GetContext();
  99. var request = context.Request;
  100. var url = request.Url;
  101. context.Response.AddHeader("Cache-Control", "no-cache");
  102. if (string.IsNullOrEmpty(url.LocalPath) || url.LocalPath == "/")
  103. {
  104. var responseText = HtmlResponseText.Replace("###SCENE###", SceneFilename+"?once="+r.Next());
  105. WriteResponse(context, responseText);
  106. }
  107. else
  108. {
  109. try
  110. {
  111. var path = Path.Combine(SceneFolder, HttpUtility.UrlDecode(url.PathAndQuery.Substring(1)));
  112. var questionMarkIndex = path.IndexOf("?");
  113. if (questionMarkIndex != -1)
  114. {
  115. path = path.Substring(0, questionMarkIndex);
  116. }
  117. var hashIndex = path.IndexOf("#");
  118. if (hashIndex != -1)
  119. {
  120. path = path.Substring(0, hashIndex);
  121. }
  122. var buffer = File.ReadAllBytes(path);
  123. WriteResponse(context, buffer);
  124. }
  125. catch
  126. {
  127. context.Response.StatusCode = 404;
  128. context.Response.Close();
  129. }
  130. }
  131. }
  132. }
  133. catch
  134. {
  135. }
  136. }
  137. static void WriteResponse(HttpListenerContext context, string s)
  138. {
  139. WriteResponse(context.Response, s);
  140. }
  141. static void WriteResponse(HttpListenerContext context, byte[] buffer)
  142. {
  143. WriteResponse(context.Response, buffer);
  144. }
  145. static void WriteResponse(HttpListenerResponse response, string s)
  146. {
  147. byte[] buffer = Encoding.UTF8.GetBytes(s);
  148. WriteResponse(response, buffer);
  149. }
  150. static void WriteResponse(HttpListenerResponse response, byte[] buffer)
  151. {
  152. response.ContentLength64 = buffer.Length;
  153. Stream output = response.OutputStream;
  154. output.Write(buffer, 0, buffer.Length);
  155. output.Close();
  156. }
  157. }
  158. }