You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.2 KiB
C#

using System;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
namespace TestServer2
{
internal class Program
{
public static void Main(string[] args)
{
var httpsv = new HttpServer ("http://localhost:12346");
// ConfigurationManager
// Set the document root path.
httpsv.DocumentRootPath = ConfigurationManager.AppSettings["DocumentRootPath"];
// Set the HTTP GET request event.
httpsv.OnGet += (sender, e) => {
var req = e.Request;
var res = e.Response;
var path = req.RawUrl;
if (path == "/")
path += "index.html";
byte[] contents;
if (!e.TryReadFile (path, out contents)) {
res.StatusCode = (int) HttpStatusCode.NotFound;
return;
}
if (path.EndsWith (".html")) {
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
else if (path.EndsWith (".js")) {
res.ContentType = "application/javascript";
res.ContentEncoding = Encoding.UTF8;
}
res.ContentLength64 = contents.LongLength;
res.Close (contents, true);
};
// Handle endpoints
httpsv.AddWebSocketService<Test> ("/Test");
// Everything else
httpsv.Start ();
if (httpsv.IsListening) {
Console.WriteLine ("Listening on port {0}, and providing WebSocket services:", httpsv.Port);
foreach (var path in httpsv.WebSocketServices.Paths)
Console.WriteLine ("- {0}", path);
}
Console.WriteLine ("\nPress Enter key to stop the server...");
Console.ReadLine ();
httpsv.Stop ();
}
}
}