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-03-14 20:39:17 +08:00
|
|
|
//_httpsv = new HttpServer (4649, true); // For Secure Connection
|
2014-01-04 02:15:21 +08:00
|
|
|
#if DEBUG
|
2014-03-14 20:39:17 +08:00
|
|
|
// Changing logging level
|
2014-03-02 21:15:41 +08:00
|
|
|
_httpsv.Log.Level = LogLevel.Trace;
|
2014-01-04 02:15:21 +08:00
|
|
|
#endif
|
2014-03-14 20:39:17 +08:00
|
|
|
/* For Secure Connection
|
2014-01-04 02:15:21 +08:00
|
|
|
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
|
|
|
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
|
|
|
_httpsv.Certificate = new X509Certificate2 (cert, password);
|
|
|
|
*/
|
|
|
|
|
2014-03-14 20:39:17 +08:00
|
|
|
/* For HTTP Authentication (Basic/Digest)
|
2014-01-04 02:15:21 +08:00
|
|
|
_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;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2014-03-14 20:39:17 +08:00
|
|
|
// Not to remove inactive clients in WebSocket services periodically
|
2013-07-30 15:00:17 +08:00
|
|
|
//_httpsv.KeepClean = false;
|
2014-01-01 20:43:18 +08:00
|
|
|
|
2014-03-14 20:39:17 +08:00
|
|
|
// Setting document root path
|
2014-01-04 03:09:23 +08:00
|
|
|
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
|
|
|
|
2014-03-14 20:39:17 +08:00
|
|
|
// Setting HTTP method events
|
2014-01-04 02:15:21 +08:00
|
|
|
_httpsv.OnGet += (sender, e) => onGet (e);
|
|
|
|
|
2014-03-14 20:39:17 +08:00
|
|
|
// Adding WebSocket services
|
2013-07-24 16:23:48 +08:00
|
|
|
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
|
|
|
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
2014-03-07 20:15:55 +08:00
|
|
|
|
|
|
|
/* With initializing
|
|
|
|
_httpsv.AddWebSocketService<Chat> (
|
|
|
|
"/Chat",
|
|
|
|
() => new Chat ("Anon#") {
|
|
|
|
Protocol = "chat",
|
2014-03-14 20:39:17 +08:00
|
|
|
// Checking Origin header
|
2014-03-07 20:15:55 +08:00
|
|
|
OriginValidator = value => {
|
|
|
|
Uri origin;
|
|
|
|
return !value.IsNullOrEmpty () &&
|
|
|
|
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
|
|
|
|
origin.Host == "localhost";
|
|
|
|
},
|
2014-03-14 20:39:17 +08:00
|
|
|
// Checking Cookies
|
2014-03-07 20:15:55 +08:00
|
|
|
CookiesValidator = (req, res) => {
|
|
|
|
foreach (Cookie cookie in req) {
|
|
|
|
cookie.Expired = true;
|
|
|
|
res.Add (cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
*/
|
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-03-14 20:39:17 +08:00
|
|
|
"An HTTP server listening on port: {0}, providing WebSocket services:", _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)
|
2014-03-14 20:39:17 +08:00
|
|
|
Console.WriteLine ("- {0}", path);
|
2013-08-27 21:02:50 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2014-03-14 20:39:17 +08:00
|
|
|
private static void onGet (HttpRequestEventArgs e)
|
2012-09-10 00:36:22 +08:00
|
|
|
{
|
2014-03-14 20:39:17 +08:00
|
|
|
var req = e.Request;
|
|
|
|
var res = e.Response;
|
2014-01-04 02:15:21 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|