From 54be7d0168c615375a40575c1ea0abfd4faea314 Mon Sep 17 00:00:00 2001 From: sta Date: Sat, 4 Jan 2014 03:15:21 +0900 Subject: [PATCH] Refactored Example3 --- Example3/Chat.cs | 2 +- Example3/Echo.cs | 6 +++--- Example3/Program.cs | 44 ++++++++++++++++++++++---------------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Example3/Chat.cs b/Example3/Chat.cs index ae8d7907..77a67879 100644 --- a/Example3/Chat.cs +++ b/Example3/Chat.cs @@ -27,7 +27,7 @@ namespace Example3 return Context.QueryString ["name"] ?? (_prefix + getNum ()); } - private int getNum () + private static int getNum () { return Interlocked.Increment (ref _num); } diff --git a/Example3/Echo.cs b/Example3/Echo.cs index b2515433..c15d6da5 100644 --- a/Example3/Echo.cs +++ b/Example3/Echo.cs @@ -8,9 +8,9 @@ namespace Example3 { protected override void OnMessage (MessageEventArgs e) { - var name = Context.QueryString ["name"]; - var msg = name != null - ? String.Format ("Returns '{0}' to {1}", e.Data, name) + var name = Context.QueryString ["name"] ?? String.Empty; + var msg = name.Length > 0 + ? String.Format ("'{0}' to {1}", e.Data, name) : e.Data; Send (msg); diff --git a/Example3/Program.cs b/Example3/Program.cs index 3a1d8e48..ee1f4339 100644 --- a/Example3/Program.cs +++ b/Example3/Program.cs @@ -14,15 +14,22 @@ namespace Example3 public static void Main (string [] args) { _httpsv = new HttpServer (4649); - //_httpsv = new HttpServer (4649, true); - #if DEBUG + //_httpsv = new HttpServer (4649, true) // Secure; + +#if DEBUG _httpsv.Log.Level = LogLevel.TRACE; - #endif +#endif + _httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"]; - // HTTP Basic/Digest Authentication - /* - _httpsv.AuthenticationSchemes = AuthenticationSchemes.Digest; + /* 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; _httpsv.Realm = "WebSocket Test"; _httpsv.UserCredentialsFinder = identity => { var name = identity.Name; @@ -32,25 +39,18 @@ namespace Example3 }; */ - // Secure Connection - /* - var cert = ConfigurationManager.AppSettings ["ServerCertFile"]; - var password = ConfigurationManager.AppSettings ["CertFilePassword"]; - _httpsv.Certificate = new X509Certificate2 (cert, password); - */ - //_httpsv.KeepClean = false; + _httpsv.OnGet += (sender, e) => onGet (e); + _httpsv.AddWebSocketService ("/Echo"); _httpsv.AddWebSocketService ("/Chat"); //_httpsv.AddWebSocketService ("/Chat", () => new Chat ("Anon#")); - _httpsv.OnGet += (sender, e) => onGet (e); - _httpsv.Start (); if (_httpsv.IsListening) { Console.WriteLine ( - "An HTTP Server listening on port: {0} WebSocket service paths:", + "An HTTP server listening on port: {0} WebSocket service paths:", _httpsv.Port); foreach (var path in _httpsv.WebSocketServices.ServicePaths) @@ -60,7 +60,7 @@ namespace Example3 Console.WriteLine ("\nPress Enter key to stop the server..."); Console.ReadLine (); - _httpsv.Stop (); + _httpsv.Stop (); } private static byte [] getContent (string path) @@ -73,15 +73,15 @@ namespace Example3 private static void onGet (HttpRequestEventArgs eventArgs) { - var request = eventArgs.Request; - var response = eventArgs.Response; - var content = getContent (request.RawUrl); + var req = eventArgs.Request; + var res = eventArgs.Response; + var content = getContent (req.RawUrl); if (content != null) { - response.WriteContent (content); + res.WriteContent (content); return; } - response.StatusCode = (int) HttpStatusCode.NotFound; + res.StatusCode = (int) HttpStatusCode.NotFound; } } }