WebServer.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/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. </style>
  29. </head>
  30. <body>
  31. <canvas id='canvas'></canvas>
  32. <script type='text/javascript'>
  33. var canvas = document.getElementById('canvas');
  34. var engine = new BABYLON.Engine(canvas, true);
  35. BABYLON.SceneLoader.Load('', '###SCENE###', engine, function (newScene) {
  36. newScene.activeCamera.attachControl(canvas);
  37. engine.runRenderLoop(function() {
  38. newScene.render();
  39. });
  40. window.addEventListener('resize', function () {
  41. engine.resize();
  42. });
  43. });
  44. </script>
  45. </body>
  46. </html>";
  47. public const int Port = 45478;
  48. public static bool IsSupported { get; private set; }
  49. static WebServer()
  50. {
  51. try
  52. {
  53. listener = new HttpListener();
  54. if (!HttpListener.IsSupported)
  55. {
  56. IsSupported = false;
  57. return;
  58. }
  59. listener.Prefixes.Add("http://localhost:" + Port + "/");
  60. listener.Start();
  61. runningTask = Task.Run(() => Listen());
  62. IsSupported = true;
  63. }
  64. catch
  65. {
  66. IsSupported = false;
  67. }
  68. }
  69. public static string SceneFilename { get; set; }
  70. public static string SceneFolder { get; set; }
  71. static Random r = new Random();
  72. static void Listen()
  73. {
  74. try
  75. {
  76. while (listener.IsListening)
  77. {
  78. var context = listener.GetContext();
  79. var request = context.Request;
  80. var url = request.Url;
  81. context.Response.AddHeader("Cache-Control", "no-cache");
  82. if (string.IsNullOrEmpty(url.LocalPath) || url.LocalPath == "/")
  83. {
  84. var responseText = HtmlResponseText.Replace("###SCENE###", SceneFilename+"?once="+r.Next());
  85. WriteResponse(context, responseText);
  86. }
  87. else
  88. {
  89. try
  90. {
  91. var path = Path.Combine(SceneFolder, HttpUtility.UrlDecode(url.PathAndQuery.Substring(1)));
  92. var questionMarkIndex = path.IndexOf("?");
  93. if (questionMarkIndex != -1)
  94. {
  95. path = path.Substring(0, questionMarkIndex);
  96. }
  97. var hashIndex = path.IndexOf("#");
  98. if (hashIndex != -1)
  99. {
  100. path = path.Substring(0, hashIndex);
  101. }
  102. var buffer = File.ReadAllBytes(path);
  103. WriteResponse(context, buffer);
  104. }
  105. catch
  106. {
  107. context.Response.StatusCode = 404;
  108. context.Response.Close();
  109. }
  110. }
  111. }
  112. }
  113. catch
  114. {
  115. }
  116. }
  117. static void WriteResponse(HttpListenerContext context, string s)
  118. {
  119. WriteResponse(context.Response, s);
  120. }
  121. static void WriteResponse(HttpListenerContext context, byte[] buffer)
  122. {
  123. WriteResponse(context.Response, buffer);
  124. }
  125. static void WriteResponse(HttpListenerResponse response, string s)
  126. {
  127. byte[] buffer = Encoding.UTF8.GetBytes(s);
  128. WriteResponse(response, buffer);
  129. }
  130. static void WriteResponse(HttpListenerResponse response, byte[] buffer)
  131. {
  132. response.ContentLength64 = buffer.Length;
  133. Stream output = response.OutputStream;
  134. output.Write(buffer, 0, buffer.Length);
  135. output.Close();
  136. }
  137. }
  138. }