2012-09-10 00:36:22 +08:00
|
|
|
using System;
|
|
|
|
using WebSocketSharp;
|
|
|
|
using WebSocketSharp.Net;
|
|
|
|
using WebSocketSharp.Server;
|
|
|
|
|
|
|
|
namespace Example3
|
|
|
|
{
|
|
|
|
public class Program
|
|
|
|
{
|
2012-09-19 13:40:12 +08:00
|
|
|
private static HttpServer _httpsv;
|
2012-09-10 00:36:22 +08:00
|
|
|
|
|
|
|
public static void Main(string[] args)
|
|
|
|
{
|
2012-09-19 13:40:12 +08:00
|
|
|
_httpsv = new HttpServer(4649);
|
2013-07-15 19:42:55 +08:00
|
|
|
#if DEBUG
|
|
|
|
_httpsv.Log.Level = LogLevel.TRACE;
|
|
|
|
#endif
|
2013-07-04 14:48:01 +08:00
|
|
|
//_httpsv.RootPath = "../../Public";
|
2013-06-11 15:26:03 +08:00
|
|
|
//_httpsv.Sweeping = false;
|
2013-02-11 21:57:01 +08:00
|
|
|
_httpsv.AddWebSocketService<Echo>("/Echo");
|
|
|
|
_httpsv.AddWebSocketService<Chat>("/Chat");
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-02-18 15:47:33 +08:00
|
|
|
_httpsv.OnGet += (sender, e) =>
|
2012-09-10 00:36:22 +08:00
|
|
|
{
|
2013-02-18 15:47:33 +08:00
|
|
|
onGet(e);
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_httpsv.OnError += (sender, e) =>
|
|
|
|
{
|
|
|
|
Console.WriteLine(e.Message);
|
|
|
|
};
|
|
|
|
|
|
|
|
_httpsv.Start();
|
2012-11-02 16:55:00 +08:00
|
|
|
Console.WriteLine("HTTP Server listening on port: {0} service path:", _httpsv.Port);
|
2013-01-28 18:42:47 +08:00
|
|
|
foreach (var path in _httpsv.ServicePaths)
|
2012-11-02 16:55:00 +08:00
|
|
|
Console.WriteLine(" {0}", path);
|
|
|
|
Console.WriteLine();
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-07-15 19:42:55 +08:00
|
|
|
Console.WriteLine("Press enter key to stop server...");
|
2012-09-10 00:36:22 +08:00
|
|
|
Console.ReadLine();
|
|
|
|
|
|
|
|
_httpsv.Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private static byte[] getContent(string path)
|
|
|
|
{
|
|
|
|
if (path == "/")
|
|
|
|
path += "index.html";
|
|
|
|
|
|
|
|
return _httpsv.GetFile(path);
|
|
|
|
}
|
|
|
|
|
2013-02-18 15:47:33 +08:00
|
|
|
private static void onGet(HttpRequestEventArgs eventArgs)
|
2012-09-10 00:36:22 +08:00
|
|
|
{
|
2013-02-11 21:57:01 +08:00
|
|
|
var request = eventArgs.Request;
|
|
|
|
var response = eventArgs.Response;
|
|
|
|
var content = getContent(request.RawUrl);
|
2012-09-10 00:36:22 +08:00
|
|
|
if (content != null)
|
|
|
|
{
|
|
|
|
response.WriteContent(content);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.StatusCode = (int)HttpStatusCode.NotFound;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|