|
|
|
@ -12,21 +12,20 @@ namespace Example2
|
|
|
|
public static void Main (string [] args)
|
|
|
|
public static void Main (string [] args)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
var wssv = new WebSocketServer (4649);
|
|
|
|
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 ("ws://localhost:4649");
|
|
|
|
//var wssv = new WebSocketServer ("wss://localhost:4649"); // Secure
|
|
|
|
//var wssv = new WebSocketServer ("wss://localhost:4649"); // For Secure Connection
|
|
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
// Changing logging level
|
|
|
|
wssv.Log.Level = LogLevel.Trace;
|
|
|
|
wssv.Log.Level = LogLevel.Trace;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* For Secure Connection
|
|
|
|
/* 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);
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* HTTP Authentication (Basic/Digest)
|
|
|
|
/* For 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 => {
|
|
|
|
@ -37,42 +36,26 @@ namespace Example2
|
|
|
|
};
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Not to remove inactive clients periodically
|
|
|
|
//wssv.KeepClean = false;
|
|
|
|
//wssv.KeepClean = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Adding WebSocket services
|
|
|
|
wssv.AddWebSocketService<Echo> ("/Echo");
|
|
|
|
wssv.AddWebSocketService<Echo> ("/Echo");
|
|
|
|
wssv.AddWebSocketService<Chat> ("/Chat");
|
|
|
|
wssv.AddWebSocketService<Chat> ("/Chat");
|
|
|
|
|
|
|
|
|
|
|
|
/* With initializing
|
|
|
|
/* With initializing
|
|
|
|
wssv.AddWebSocketService<Echo> (
|
|
|
|
|
|
|
|
"/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> (
|
|
|
|
wssv.AddWebSocketService<Chat> (
|
|
|
|
"/Chat",
|
|
|
|
"/Chat",
|
|
|
|
() => new Chat ("Anon#") {
|
|
|
|
() => new Chat ("Anon#") {
|
|
|
|
Protocol = "chat",
|
|
|
|
Protocol = "chat",
|
|
|
|
|
|
|
|
// Checking 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
|
|
|
|
CookiesValidator = (req, res) => {
|
|
|
|
CookiesValidator = (req, res) => {
|
|
|
|
foreach (Cookie cookie in req) {
|
|
|
|
foreach (Cookie cookie in req) {
|
|
|
|
cookie.Expired = true;
|
|
|
|
cookie.Expired = true;
|
|
|
|
@ -87,13 +70,13 @@ namespace Example2
|
|
|
|
wssv.Start ();
|
|
|
|
wssv.Start ();
|
|
|
|
if (wssv.IsListening) {
|
|
|
|
if (wssv.IsListening) {
|
|
|
|
Console.WriteLine (
|
|
|
|
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)
|
|
|
|
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 ();
|
|
|
|
Console.ReadLine ();
|
|
|
|
|
|
|
|
|
|
|
|
wssv.Stop ();
|
|
|
|
wssv.Stop ();
|
|
|
|
|