websocket-sharp/Example2/Program.cs

95 lines
3.0 KiB
C#
Raw Normal View History

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;
using WebSocketSharp.Net;
2012-08-06 13:34:39 +08:00
using WebSocketSharp.Server;
2012-08-04 14:51:31 +08:00
namespace Example2
{
public class Program
{
2014-10-03 14:32:50 +08:00
public static void Main (string[] args)
2012-08-04 14:51:31 +08:00
{
2014-10-03 14:32:50 +08:00
/* 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.
*/
2013-07-19 16:29:58 +08:00
var wssv = new WebSocketServer (4649);
2014-10-03 14:32:50 +08:00
//var wssv = new WebSocketServer (4649, true);
2013-07-19 16:29:58 +08:00
//var wssv = new WebSocketServer ("ws://localhost:4649");
2014-10-03 14:32:50 +08:00
//var wssv = new WebSocketServer ("wss://localhost:4649");
2014-01-03 14:40:38 +08:00
#if DEBUG
2014-10-03 14:32:50 +08:00
// To change the logging level.
wssv.Log.Level = LogLevel.Trace;
2014-10-03 14:32:50 +08:00
// To change the wait time for the response to the WebSocket Ping or Close.
wssv.WaitTime = TimeSpan.FromSeconds (2);
2014-01-03 14:40:38 +08:00
#endif
2014-10-03 14:32:50 +08:00
/* To provide the secure connection.
var cert = ConfigurationManager.AppSettings["ServerCertFile"];
var password = ConfigurationManager.AppSettings["CertFilePassword"];
2014-01-03 14:40:38 +08:00
wssv.Certificate = new X509Certificate2 (cert, password);
*/
2014-10-03 14:32:50 +08:00
/* To provide the HTTP Authentication (Basic/Digest).
2014-01-03 14:40:38 +08:00
wssv.AuthenticationSchemes = AuthenticationSchemes.Basic;
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")
: null;
};
*/
2014-10-06 13:18:20 +08:00
// Not to remove the inactive sessions periodically.
//wssv.KeepClean = false;
2014-10-03 14:32:50 +08:00
// To resolve to wait for socket in TIME_WAIT state.
//wssv.ReuseAddress = true;
2014-10-03 14:32:50 +08:00
// Add the WebSocket services.
2013-07-19 16:29:58 +08:00
wssv.AddWebSocketService<Echo> ("/Echo");
wssv.AddWebSocketService<Chat> ("/Chat");
2014-10-03 14:32:50 +08:00
/* Add the WebSocket service with initializing.
wssv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat ("Anon#") {
Protocol = "chat",
2014-10-03 14:32:50 +08:00
// To validate the Origin header.
OriginValidator = value => {
Uri origin;
return !value.IsNullOrEmpty () &&
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
origin.Host == "localhost";
},
2014-10-03 14:32:50 +08:00
// To validate the Cookies.
CookiesValidator = (req, res) => {
foreach (Cookie cookie in req) {
cookie.Expired = true;
res.Add (cookie);
}
return true;
}
});
*/
2013-07-19 16:29:58 +08:00
wssv.Start ();
if (wssv.IsListening) {
2014-10-03 14:32:50 +08:00
Console.WriteLine ("Listening on port {0}, providing services:", wssv.Port);
foreach (var path in wssv.WebSocketServices.Paths)
2014-03-14 19:56:14 +08:00
Console.WriteLine ("- {0}", path);
}
2012-08-04 14:51:31 +08:00
2014-03-14 19:56:14 +08:00
Console.WriteLine ("\nPress Enter key to stop the 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
}
}
}