From e775980454575256bf376c87f8f00e5696d49b87 Mon Sep 17 00:00:00 2001 From: sta Date: Fri, 14 Mar 2014 20:56:14 +0900 Subject: [PATCH] Modified Example2 --- Example2/AssemblyInfo.cs | 2 +- Example2/Echo.cs | 5 ++--- Example2/Program.cs | 41 ++++++++++++---------------------------- 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/Example2/AssemblyInfo.cs b/Example2/AssemblyInfo.cs index 6335efbf..f5ad9e1c 100644 --- a/Example2/AssemblyInfo.cs +++ b/Example2/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/Example2/Echo.cs b/Example2/Echo.cs index bc4694ff..c3413896 100644 --- a/Example2/Echo.cs +++ b/Example2/Echo.cs @@ -1,6 +1,5 @@ using System; using WebSocketSharp; -using WebSocketSharp.Net; using WebSocketSharp.Server; namespace Example2 @@ -9,8 +8,8 @@ namespace Example2 { 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/Example2/Program.cs b/Example2/Program.cs index 197ad7fc..8f231dc5 100644 --- a/Example2/Program.cs +++ b/Example2/Program.cs @@ -12,21 +12,20 @@ namespace Example2 public static void Main (string [] args) { var wssv = new WebSocketServer (4649); - //var wssv = new WebSocketServer (4649, true); // Secure + //var wssv = new WebSocketServer (4649, true); // For Secure Connection //var wssv = new WebSocketServer ("ws://localhost:4649"); - //var wssv = new WebSocketServer ("wss://localhost:4649"); // Secure - + //var wssv = new WebSocketServer ("wss://localhost:4649"); // For Secure Connection #if DEBUG + // Changing logging level wssv.Log.Level = LogLevel.Trace; #endif - - /* Secure Connection + /* For Secure Connection var cert = ConfigurationManager.AppSettings ["ServerCertFile"]; var password = ConfigurationManager.AppSettings ["CertFilePassword"]; wssv.Certificate = new X509Certificate2 (cert, password); */ - /* HTTP Authentication (Basic/Digest) + /* For HTTP Authentication (Basic/Digest) wssv.AuthenticationSchemes = AuthenticationSchemes.Basic; wssv.Realm = "WebSocket Test"; wssv.UserCredentialsFinder = identity => { @@ -37,42 +36,26 @@ namespace Example2 }; */ + // Not to remove inactive clients periodically //wssv.KeepClean = false; + // Adding WebSocket services wssv.AddWebSocketService ("/Echo"); wssv.AddWebSocketService ("/Chat"); /* With initializing - wssv.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; - } - }); - wssv.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; @@ -87,13 +70,13 @@ namespace Example2 wssv.Start (); if (wssv.IsListening) { Console.WriteLine ( - "A WebSocket server listening on port: {0} service paths:", wssv.Port); + "A WebSocket server listening on port: {0}, providing services:", wssv.Port); foreach (var path in wssv.WebSocketServices.Paths) - Console.WriteLine (" {0}", path); + Console.WriteLine ("- {0}", path); } - Console.WriteLine ("\nPress Enter key to stop server..."); + Console.WriteLine ("\nPress Enter key to stop the server..."); Console.ReadLine (); wssv.Stop ();