2012-08-04 14:51:31 +08:00
|
|
|
using System;
|
2013-07-19 16:29:58 +08:00
|
|
|
using System.Configuration;
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2013-07-15 19:42:55 +08:00
|
|
|
using WebSocketSharp;
|
2014-01-01 20:43:18 +08:00
|
|
|
using WebSocketSharp.Net;
|
2012-08-06 13:34:39 +08:00
|
|
|
using WebSocketSharp.Server;
|
2012-08-04 14:51:31 +08:00
|
|
|
|
2013-09-25 19:52:07 +08:00
|
|
|
namespace Example2
|
|
|
|
{
|
|
|
|
public class Program
|
|
|
|
{
|
2013-07-19 16:29:58 +08:00
|
|
|
public static void Main (string [] args)
|
2012-08-04 14:51:31 +08:00
|
|
|
{
|
2013-07-19 16:29:58 +08:00
|
|
|
var wssv = new WebSocketServer (4649);
|
2014-01-03 14:40:38 +08:00
|
|
|
//var wssv = new WebSocketServer (4649, true); // Secure
|
2013-07-19 16:29:58 +08:00
|
|
|
//var wssv = new WebSocketServer ("ws://localhost:4649");
|
2014-01-03 14:40:38 +08:00
|
|
|
//var wssv = new WebSocketServer ("wss://localhost:4649"); // Secure
|
|
|
|
|
|
|
|
#if DEBUG
|
2013-07-15 19:42:55 +08:00
|
|
|
wssv.Log.Level = LogLevel.TRACE;
|
2014-01-03 14:40:38 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Secure Connection
|
|
|
|
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
|
|
|
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
|
|
|
wssv.Certificate = new X509Certificate2 (cert, password);
|
|
|
|
*/
|
2014-01-01 20:43:18 +08:00
|
|
|
|
2014-01-03 14:40:38 +08:00
|
|
|
/* HTTP Authentication (Basic/Digest)
|
|
|
|
wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
2014-01-01 20:43:18 +08:00
|
|
|
wssv.Realm = "WebSocket Test";
|
|
|
|
wssv.UserCredentialsFinder = identity => {
|
2014-01-27 14:39:27 +08:00
|
|
|
var expected = "nobita";
|
|
|
|
return identity.Name == expected
|
|
|
|
? new NetworkCredential (expected, "password", "gunfighter")
|
2014-01-01 20:43:18 +08:00
|
|
|
: null;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2013-07-30 15:00:17 +08:00
|
|
|
//wssv.KeepClean = false;
|
2014-01-01 20:43:18 +08:00
|
|
|
|
2013-07-19 16:29:58 +08:00
|
|
|
wssv.AddWebSocketService<Echo> ("/Echo");
|
|
|
|
wssv.AddWebSocketService<Chat> ("/Chat");
|
2014-02-07 15:47:59 +08:00
|
|
|
//wssv.AddWebSocketService<Chat> (
|
|
|
|
// "/Chat",
|
|
|
|
// () => new Chat ("Anon#") { Protocol = "chat" });
|
2013-07-19 16:29:58 +08:00
|
|
|
|
|
|
|
wssv.Start ();
|
2014-01-01 20:43:18 +08:00
|
|
|
if (wssv.IsListening) {
|
2013-10-14 19:38:15 +08:00
|
|
|
Console.WriteLine (
|
2014-01-03 14:52:47 +08:00
|
|
|
"A WebSocket server listening on port: {0} service paths:", wssv.Port);
|
2013-10-14 19:38:15 +08:00
|
|
|
|
2014-02-13 13:38:13 +08:00
|
|
|
foreach (var path in wssv.WebSocketServices.Paths)
|
2013-10-14 19:38:15 +08:00
|
|
|
Console.WriteLine (" {0}", path);
|
|
|
|
}
|
2012-08-04 14:51:31 +08:00
|
|
|
|
2013-10-12 19:42:11 +08:00
|
|
|
Console.WriteLine ("\nPress Enter key to stop server...");
|
2013-07-19 16:29:58 +08:00
|
|
|
Console.ReadLine ();
|
2012-08-04 14:51:31 +08:00
|
|
|
|
2013-07-19 16:29:58 +08:00
|
|
|
wssv.Stop ();
|
2012-08-04 14:51:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|