websocket-sharp/Example3/Program.cs

69 lines
1.6 KiB
C#
Raw Normal View History

2012-09-10 00:36:22 +08:00
using System;
using System.Configuration;
2012-09-10 00:36:22 +08:00
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
namespace Example3
{
public class Program
{
private static HttpServer _httpsv;
2012-09-10 00:36:22 +08:00
public static void Main (string [] args)
2012-09-10 00:36:22 +08:00
{
_httpsv = new HttpServer (4649);
2013-07-15 19:42:55 +08:00
#if DEBUG
_httpsv.Log.Level = LogLevel.TRACE;
#endif
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
2013-06-11 15:26:03 +08:00
//_httpsv.Sweeping = false;
_httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat");
2012-09-10 00:36:22 +08:00
_httpsv.OnGet += (sender, e) =>
2012-09-10 00:36:22 +08:00
{
onGet (e);
2012-09-10 00:36:22 +08:00
};
_httpsv.OnError += (sender, e) =>
{
Console.WriteLine (e.Message);
2012-09-10 00:36:22 +08:00
};
_httpsv.Start ();
Console.WriteLine ("HTTP Server listening on port: {0} service path:", _httpsv.Port);
foreach (var path in _httpsv.ServicePaths)
Console.WriteLine (" {0}", path);
Console.WriteLine ();
2012-09-10 00:36:22 +08:00
Console.WriteLine ("Press enter key to stop the server...");
Console.ReadLine ();
2012-09-10 00:36:22 +08:00
_httpsv.Stop ();
2012-09-10 00:36:22 +08:00
}
private static byte [] getContent (string path)
2012-09-10 00:36:22 +08:00
{
if (path == "/")
path += "index.html";
return _httpsv.GetFile (path);
2012-09-10 00:36:22 +08:00
}
private static void onGet (HttpRequestEventArgs eventArgs)
2012-09-10 00:36:22 +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);
2012-09-10 00:36:22 +08:00
return;
}
response.StatusCode = (int) HttpStatusCode.NotFound;
2012-09-10 00:36:22 +08:00
}
}
}