WebServer.cs 5.4 KB

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