Modified a few for Example2

This commit is contained in:
sta 2014-10-03 15:32:50 +09:00
parent 0fc7c82e70
commit ca4fdabb2e
4 changed files with 34 additions and 31 deletions

View File

@ -24,4 +24,3 @@ using System.Runtime.CompilerServices;
//[assembly: AssemblyDelaySign(false)] //[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")] //[assembly: AssemblyKeyFile("")]

View File

@ -7,10 +7,9 @@ namespace Example2
{ {
public class Chat : WebSocketBehavior public class Chat : WebSocketBehavior
{ {
private static int _num = 0; private string _name;
private static int _number = 0;
private string _name; private string _prefix;
private string _prefix;
public Chat () public Chat ()
: this (null) : this (null)
@ -24,13 +23,15 @@ namespace Example2
private string getName () private string getName ()
{ {
var name = Context.QueryString ["name"]; var name = Context.QueryString["name"];
return !name.IsNullOrEmpty () ? name : (_prefix + getNum ()); return !name.IsNullOrEmpty ()
? name
: (_prefix + getNumber ());
} }
private static int getNum () private static int getNumber ()
{ {
return Interlocked.Increment (ref _num); return Interlocked.Increment (ref _number);
} }
protected override void OnOpen () protected override void OnOpen ()

View File

@ -8,11 +8,8 @@ namespace Example2
{ {
protected override void OnMessage (MessageEventArgs e) protected override void OnMessage (MessageEventArgs e)
{ {
var name = Context.QueryString ["name"]; var name = Context.QueryString["name"];
var msg = !name.IsNullOrEmpty () var msg = !name.IsNullOrEmpty () ? String.Format ("'{0}' to {1}", e.Data, name) : e.Data;
? String.Format ("'{0}' to {1}", e.Data, name)
: e.Data;
Send (msg); Send (msg);
} }
} }

View File

@ -9,23 +9,31 @@ namespace Example2
{ {
public class Program public class Program
{ {
public static void Main (string [] args) public static void Main (string[] args)
{ {
/* Create a new instance of the WebSocketServer class.
*
* If you would like to provide the secure connection, you should create the instance
* with the 'secure' parameter set to true, or the wss scheme WebSocket URL.
*/
var wssv = new WebSocketServer (4649); var wssv = new WebSocketServer (4649);
//var wssv = new WebSocketServer (4649, true); // For Secure Connection //var wssv = new WebSocketServer (4649, true);
//var wssv = new WebSocketServer ("ws://localhost:4649"); //var wssv = new WebSocketServer ("ws://localhost:4649");
//var wssv = new WebSocketServer ("wss://localhost:4649"); // For Secure Connection //var wssv = new WebSocketServer ("wss://localhost:4649");
#if DEBUG #if DEBUG
// Changing the logging level // To change the logging level.
wssv.Log.Level = LogLevel.Trace; wssv.Log.Level = LogLevel.Trace;
// To change the wait time for the response to the WebSocket Ping or Close.
wssv.WaitTime = TimeSpan.FromSeconds (2);
#endif #endif
/* For Secure Connection /* To provide the secure connection.
var cert = ConfigurationManager.AppSettings ["ServerCertFile"]; var cert = ConfigurationManager.AppSettings["ServerCertFile"];
var password = ConfigurationManager.AppSettings ["CertFilePassword"]; var password = ConfigurationManager.AppSettings["CertFilePassword"];
wssv.Certificate = new X509Certificate2 (cert, password); wssv.Certificate = new X509Certificate2 (cert, password);
*/ */
/* For HTTP Authentication (Basic/Digest) /* To provide the HTTP Authentication (Basic/Digest).
wssv.AuthenticationSchemes = AuthenticationSchemes.Basic; wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
wssv.Realm = "WebSocket Test"; wssv.Realm = "WebSocket Test";
wssv.UserCredentialsFinder = identity => { wssv.UserCredentialsFinder = identity => {
@ -36,29 +44,29 @@ namespace Example2
}; };
*/ */
// Not to remove inactive clients periodically // Not to remove inactive sessions periodically.
//wssv.KeepClean = false; //wssv.KeepClean = false;
// To resolve to wait for socket in TIME_WAIT state // To resolve to wait for socket in TIME_WAIT state.
//wssv.ReuseAddress = true; //wssv.ReuseAddress = true;
// Adding WebSocket services // Add the WebSocket services.
wssv.AddWebSocketService<Echo> ("/Echo"); wssv.AddWebSocketService<Echo> ("/Echo");
wssv.AddWebSocketService<Chat> ("/Chat"); wssv.AddWebSocketService<Chat> ("/Chat");
/* With initializing /* Add the WebSocket service with initializing.
wssv.AddWebSocketService<Chat> ( wssv.AddWebSocketService<Chat> (
"/Chat", "/Chat",
() => new Chat ("Anon#") { () => new Chat ("Anon#") {
Protocol = "chat", Protocol = "chat",
// Checking Origin header // To validate the Origin header.
OriginValidator = value => { OriginValidator = value => {
Uri origin; Uri origin;
return !value.IsNullOrEmpty () && return !value.IsNullOrEmpty () &&
Uri.TryCreate (value, UriKind.Absolute, out origin) && Uri.TryCreate (value, UriKind.Absolute, out origin) &&
origin.Host == "localhost"; origin.Host == "localhost";
}, },
// Checking Cookies // To validate the Cookies.
CookiesValidator = (req, res) => { CookiesValidator = (req, res) => {
foreach (Cookie cookie in req) { foreach (Cookie cookie in req) {
cookie.Expired = true; cookie.Expired = true;
@ -72,9 +80,7 @@ namespace Example2
wssv.Start (); wssv.Start ();
if (wssv.IsListening) { if (wssv.IsListening) {
Console.WriteLine ( Console.WriteLine ("Listening on port {0}, providing services:", wssv.Port);
"A WebSocket server listening on port: {0}, providing services:", wssv.Port);
foreach (var path in wssv.WebSocketServices.Paths) foreach (var path in wssv.WebSocketServices.Paths)
Console.WriteLine ("- {0}", path); Console.WriteLine ("- {0}", path);
} }