2012-09-10 00:36:22 +08:00
|
|
|
using System;
|
2013-07-24 16:23:48 +08:00
|
|
|
using System.Configuration;
|
2013-08-27 21:02:50 +08:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
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);
|
2014-01-04 02:15:21 +08:00
|
|
|
//_httpsv = new HttpServer (4649, true) // Secure;
|
|
|
|
|
|
|
|
#if DEBUG
|
2013-07-15 19:42:55 +08:00
|
|
|
_httpsv.Log.Level = LogLevel.TRACE;
|
2014-01-04 02:15:21 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Secure Connection
|
|
|
|
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
|
|
|
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
|
|
|
_httpsv.Certificate = new X509Certificate2 (cert, password);
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* HTTP Authentication (Basic/Digest)
|
|
|
|
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
2014-01-01 20:43:18 +08:00
|
|
|
_httpsv.Realm = "WebSocket Test";
|
|
|
|
_httpsv.UserCredentialsFinder = identity => {
|
2014-01-27 14:39:27 +08:00
|
|
|
var expected = "nobita";
|
|
|
|
return identity.Name == expected
|
|
|
|
? new NetworkCredential (expected, "password", "gunfighter")
|
2014-01-01 20:43:18 +08:00
|
|
|
: null;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2013-07-30 15:00:17 +08:00
|
|
|
//_httpsv.KeepClean = false;
|
2014-01-01 20:43:18 +08:00
|
|
|
|
2014-01-04 03:09:23 +08:00
|
|
|
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
|
|
|
|
2014-01-04 02:15:21 +08:00
|
|
|
_httpsv.OnGet += (sender, e) => onGet (e);
|
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
|
|
|
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
2014-02-08 16:05:24 +08:00
|
|
|
//_httpsv.AddWebSocketService<Chat> (
|
|
|
|
// "/Chat",
|
|
|
|
// () => new Chat ("Anon#") { Protocol = "chat" });
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv.Start ();
|
2014-01-01 20:43:18 +08:00
|
|
|
if (_httpsv.IsListening) {
|
2013-10-14 19:38:15 +08:00
|
|
|
Console.WriteLine (
|
2014-01-04 02:15:21 +08:00
|
|
|
"An HTTP server listening on port: {0} WebSocket service paths:",
|
2014-01-01 20:43:18 +08:00
|
|
|
_httpsv.Port);
|
2013-10-14 19:38:15 +08:00
|
|
|
|
2014-02-13 13:38:13 +08:00
|
|
|
foreach (var path in _httpsv.WebSocketServices.Paths)
|
2013-08-27 21:02:50 +08:00
|
|
|
Console.WriteLine (" {0}", path);
|
|
|
|
}
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2013-10-14 19:38:15 +08:00
|
|
|
Console.WriteLine ("\nPress Enter key to stop the server...");
|
2013-07-24 16:23:48 +08:00
|
|
|
Console.ReadLine ();
|
2012-09-10 00:36:22 +08:00
|
|
|
|
2014-01-04 02:15:21 +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
|
|
|
{
|
2014-01-04 02:15:21 +08:00
|
|
|
var req = eventArgs.Request;
|
|
|
|
var res = eventArgs.Response;
|
|
|
|
var content = getContent (req.RawUrl);
|
2014-01-01 20:43:18 +08:00
|
|
|
if (content != null) {
|
2014-01-04 02:15:21 +08:00
|
|
|
res.WriteContent (content);
|
2012-09-10 00:36:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-04 02:15:21 +08:00
|
|
|
res.StatusCode = (int) HttpStatusCode.NotFound;
|
2012-09-10 00:36:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|