diff --git a/Example3/AssemblyInfo.cs b/Example3/AssemblyInfo.cs index c5396f60..a5e85b3d 100644 --- a/Example3/AssemblyInfo.cs +++ b/Example3/AssemblyInfo.cs @@ -9,7 +9,7 @@ using System.Runtime.CompilerServices; [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("")] -[assembly: AssemblyCopyright("sta")] +[assembly: AssemblyCopyright("sta.blockhead")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/Example3/Echo.cs b/Example3/Echo.cs index c15d6da5..a6c5569b 100644 --- a/Example3/Echo.cs +++ b/Example3/Echo.cs @@ -8,8 +8,8 @@ namespace Example3 { protected override void OnMessage (MessageEventArgs e) { - var name = Context.QueryString ["name"] ?? String.Empty; - var msg = name.Length > 0 + var name = Context.QueryString ["name"]; + var msg = !name.IsNullOrEmpty () ? String.Format ("'{0}' to {1}", e.Data, name) : e.Data; diff --git a/Example3/Program.cs b/Example3/Program.cs index 29343a0b..d0c4b3a1 100644 --- a/Example3/Program.cs +++ b/Example3/Program.cs @@ -14,19 +14,18 @@ namespace Example3 public static void Main (string [] args) { _httpsv = new HttpServer (4649); - //_httpsv = new HttpServer (4649, true) // Secure; - + //_httpsv = new HttpServer (4649, true); // For Secure Connection #if DEBUG + // Changing logging level _httpsv.Log.Level = LogLevel.Trace; #endif - - /* Secure Connection + /* For Secure Connection var cert = ConfigurationManager.AppSettings ["ServerCertFile"]; var password = ConfigurationManager.AppSettings ["CertFilePassword"]; _httpsv.Certificate = new X509Certificate2 (cert, password); */ - /* HTTP Authentication (Basic/Digest) + /* For HTTP Authentication (Basic/Digest) _httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic; _httpsv.Realm = "WebSocket Test"; _httpsv.UserCredentialsFinder = identity => { @@ -37,46 +36,32 @@ namespace Example3 }; */ + // Not to remove inactive clients in WebSocket services periodically //_httpsv.KeepClean = false; + // Setting document root path _httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"]; + // Setting HTTP method events _httpsv.OnGet += (sender, e) => onGet (e); + // Adding WebSocket services _httpsv.AddWebSocketService ("/Echo"); _httpsv.AddWebSocketService ("/Chat"); /* With initializing - _httpsv.AddWebSocketService ( - "/Echo", - () => new Echo () { - Protocol = "echo", - OriginValidator = value => { - Uri origin; - return !value.IsNullOrEmpty () && - Uri.TryCreate (value, UriKind.Absolute, out origin) && - origin.Host == "localhost"; - }, - CookiesValidator = (req, res) => { - foreach (Cookie cookie in req) { - cookie.Expired = true; - res.Add (cookie); - } - - return true; - } - }); - _httpsv.AddWebSocketService ( "/Chat", () => new Chat ("Anon#") { Protocol = "chat", + // Checking Origin header OriginValidator = value => { Uri origin; return !value.IsNullOrEmpty () && Uri.TryCreate (value, UriKind.Absolute, out origin) && origin.Host == "localhost"; }, + // Checking Cookies CookiesValidator = (req, res) => { foreach (Cookie cookie in req) { cookie.Expired = true; @@ -91,11 +76,10 @@ namespace Example3 _httpsv.Start (); if (_httpsv.IsListening) { Console.WriteLine ( - "An HTTP server listening on port: {0} WebSocket service paths:", - _httpsv.Port); + "An HTTP server listening on port: {0}, providing WebSocket services:", _httpsv.Port); foreach (var path in _httpsv.WebSocketServices.Paths) - Console.WriteLine (" {0}", path); + Console.WriteLine ("- {0}", path); } Console.WriteLine ("\nPress Enter key to stop the server..."); @@ -112,10 +96,10 @@ namespace Example3 return _httpsv.GetFile (path); } - private static void onGet (HttpRequestEventArgs eventArgs) + private static void onGet (HttpRequestEventArgs e) { - var req = eventArgs.Request; - var res = eventArgs.Response; + var req = e.Request; + var res = e.Response; var content = getContent (req.RawUrl); if (content != null) { res.WriteContent (content);