2012-09-10 00:36:22 +08:00
|
|
|
using System;
|
2013-07-24 16:23:48 +08:00
|
|
|
using System.Configuration;
|
2012-09-10 00:36:22 +08:00
|
|
|
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
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
public static void Main (string [] args)
|
2012-09-10 00:36:22 +08:00
|
|
|
{
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv = new HttpServer (4649);
|
2013-07-15 19:42:55 +08:00
|
|
|
#if DEBUG
|
|
|
|
_httpsv.Log.Level = LogLevel.TRACE;
|
|
|
|
#endif
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
2013-06-11 15:26:03 +08:00
|
|
|
//_httpsv.Sweeping = false;
|
2013-07-24 16:23:48 +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-07-24 16:23:48 +08:00
|
|
|
onGet (e);
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
_httpsv.OnError += (sender, e) =>
|
|
|
|
{
|
2013-07-24 16:23:48 +08:00
|
|
|
Console.WriteLine (e.Message);
|
2012-09-10 00:36:22 +08:00
|
|
|
};
|
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv.Start ();
|
|
|
|
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)
|
2013-07-24 16:23:48 +08:00
|
|
|
Console.WriteLine (" {0}", path);
|
|
|
|
Console.WriteLine ();
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
Console.WriteLine ("Press enter key to stop the server...");
|
|
|
|
Console.ReadLine ();
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv.Stop ();
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
private static byte [] getContent (string path)
|
2012-09-10 00:36:22 +08:00
|
|
|
{
|
|
|
|
if (path == "/")
|
|
|
|
path += "index.html";
|
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
return _httpsv.GetFile (path);
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
private static void onGet (HttpRequestEventArgs eventArgs)
|
2012-09-10 00:36:22 +08:00
|
|
|
{
|
2013-07-24 16:23:48 +08:00
|
|
|
var request = eventArgs.Request;
|
2013-02-11 21:57:01 +08:00
|
|
|
var response = eventArgs.Response;
|
2013-07-24 16:23:48 +08:00
|
|
|
var content = getContent (request.RawUrl);
|
2012-09-10 00:36:22 +08:00
|
|
|
if (content != null)
|
|
|
|
{
|
2013-07-24 16:23:48 +08:00
|
|
|
response.WriteContent (content);
|
2012-09-10 00:36:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
response.StatusCode = (int) HttpStatusCode.NotFound;
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|