websocket-sharp/Example3/Program.cs

56 lines
1.1 KiB
C#
Raw Normal View History

2012-09-10 00:36:22 +08:00
using System;
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
namespace Example3
{
public class Program
{
private static HttpServer<Echo> _httpsv;
public static void Main(string[] args)
{
_httpsv = new HttpServer<Echo>(4649);
2012-09-11 12:21:47 +08:00
_httpsv.OnGet += (sender, e) =>
2012-09-10 00:36:22 +08:00
{
2012-09-11 12:21:47 +08:00
onGet(e.Request, e.Response);
2012-09-10 00:36:22 +08:00
};
_httpsv.OnError += (sender, e) =>
{
Console.WriteLine(e.Message);
};
_httpsv.Start();
Console.WriteLine("HTTP Server listening on port: {0}\n", _httpsv.Port);
Console.WriteLine("Press any key to stop server...");
Console.ReadLine();
_httpsv.Stop();
}
private static byte[] getContent(string path)
{
if (path == "/")
path += "index.html";
return _httpsv.GetFile(path);
}
private static void onGet(HttpListenerRequest request, HttpListenerResponse response)
{
var content = getContent(request.RawUrl);
if (content != null)
{
response.WriteContent(content);
return;
}
response.StatusCode = (int)HttpStatusCode.NotFound;
}
}
}