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
|
|
|
|
{
|
2014-10-03 14:32:50 +08:00
|
|
|
public static void Main (string[] args)
|
2012-08-04 14:51:31 +08:00
|
|
|
{
|
2015-11-27 16:38:37 +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);
|
2015-07-15 16:38:06 +08:00
|
|
|
//var wssv = new WebSocketServer (5963, true);
|
2016-05-29 13:53:50 +08:00
|
|
|
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.Any, 4649);
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.Any, 5963, true);
|
|
|
|
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Any, 4649);
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Any, 5963, true);
|
|
|
|
|
|
|
|
//var wssv = new WebSocketServer ("ws://0.0.0.0:4649");
|
|
|
|
//var wssv = new WebSocketServer ("wss://0.0.0.0:5963");
|
|
|
|
|
|
|
|
//var wssv = new WebSocketServer ("ws://[::0]:4649");
|
|
|
|
//var wssv = new WebSocketServer ("wss://[::0]:5963");
|
|
|
|
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.Loopback, 4649);
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.Loopback, 5963, true);
|
|
|
|
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Loopback, 4649);
|
|
|
|
//var wssv = new WebSocketServer (System.Net.IPAddress.IPv6Loopback, 5963, true);
|
|
|
|
|
2013-07-19 16:29:58 +08:00
|
|
|
//var wssv = new WebSocketServer ("ws://localhost:4649");
|
2015-07-15 16:38:06 +08:00
|
|
|
//var wssv = new WebSocketServer ("wss://localhost:5963");
|
2016-05-29 13:53:50 +08:00
|
|
|
|
|
|
|
//var wssv = new WebSocketServer ("ws://127.0.0.1:4649");
|
|
|
|
//var wssv = new WebSocketServer ("wss://127.0.0.1:5963");
|
|
|
|
|
|
|
|
//var wssv = new WebSocketServer ("ws://[::1]:4649");
|
|
|
|
//var wssv = new WebSocketServer ("wss://[::1]:5963");
|
2014-01-03 14:40:38 +08:00
|
|
|
#if DEBUG
|
2014-10-03 14:32:50 +08:00
|
|
|
// To change the logging level.
|
2014-03-02 21:15:41 +08:00
|
|
|
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"];
|
2014-11-01 11:26:32 +08:00
|
|
|
var passwd = ConfigurationManager.AppSettings["CertFilePassword"];
|
|
|
|
wssv.SslConfiguration.ServerCertificate = new X509Certificate2 (cert, passwd);
|
2014-01-03 14:40:38 +08:00
|
|
|
*/
|
2014-01-01 20:43:18 +08:00
|
|
|
|
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;
|
2014-01-01 20:43:18 +08:00
|
|
|
wssv.Realm = "WebSocket Test";
|
2014-10-08 09:38:00 +08:00
|
|
|
wssv.UserCredentialsFinder = id => {
|
2014-11-18 12:26:10 +08:00
|
|
|
var name = id.Name;
|
|
|
|
|
|
|
|
// Return user name, password, and roles.
|
|
|
|
return name == "nobita"
|
|
|
|
? new NetworkCredential (name, "password", "gunfighter")
|
2014-10-08 09:38:00 +08:00
|
|
|
: null; // If the user credentials aren't found.
|
2014-01-01 20:43:18 +08:00
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2014-10-06 13:18:20 +08:00
|
|
|
// Not to remove the inactive sessions periodically.
|
2013-07-30 15:00:17 +08:00
|
|
|
//wssv.KeepClean = false;
|
2014-01-01 20:43:18 +08:00
|
|
|
|
2014-10-03 14:32:50 +08:00
|
|
|
// To resolve to wait for socket in TIME_WAIT state.
|
2014-09-04 15:03:53 +08:00
|
|
|
//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-03-07 20:15:55 +08:00
|
|
|
|
2014-10-03 14:32:50 +08:00
|
|
|
/* Add the WebSocket service with initializing.
|
2014-03-07 20:15:55 +08:00
|
|
|
wssv.AddWebSocketService<Chat> (
|
|
|
|
"/Chat",
|
|
|
|
() => new Chat ("Anon#") {
|
2015-11-27 16:38:37 +08:00
|
|
|
// To send the Sec-WebSocket-Protocol header that has a subprotocol name.
|
2014-03-07 20:15:55 +08:00
|
|
|
Protocol = "chat",
|
2015-11-27 16:38:37 +08:00
|
|
|
// To emit a WebSocket.OnMessage event when receives a ping.
|
2015-07-27 15:11:16 +08:00
|
|
|
EmitOnPing = true,
|
2015-04-16 15:02:48 +08:00
|
|
|
// To ignore the Sec-WebSocket-Extensions header.
|
|
|
|
IgnoreExtensions = true,
|
2014-10-03 14:32:50 +08:00
|
|
|
// To validate the Origin header.
|
2014-10-08 09:38:00 +08:00
|
|
|
OriginValidator = val => {
|
|
|
|
// Check the value of the Origin header, and return true if valid.
|
2014-03-07 20:15:55 +08:00
|
|
|
Uri origin;
|
2014-10-08 09:38:00 +08:00
|
|
|
return !val.IsNullOrEmpty () &&
|
|
|
|
Uri.TryCreate (val, UriKind.Absolute, out origin) &&
|
2014-03-07 20:15:55 +08:00
|
|
|
origin.Host == "localhost";
|
|
|
|
},
|
2014-10-03 14:32:50 +08:00
|
|
|
// To validate the Cookies.
|
2014-03-07 20:15:55 +08:00
|
|
|
CookiesValidator = (req, res) => {
|
2014-10-08 09:38:00 +08:00
|
|
|
// Check the Cookies in 'req', and set the Cookies to send to the client with 'res'
|
|
|
|
// if necessary.
|
2014-03-07 20:15:55 +08:00
|
|
|
foreach (Cookie cookie in req) {
|
|
|
|
cookie.Expired = true;
|
|
|
|
res.Add (cookie);
|
|
|
|
}
|
|
|
|
|
2014-10-08 09:38:00 +08:00
|
|
|
return true; // If valid.
|
2014-03-07 20:15:55 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
*/
|
2013-07-19 16:29:58 +08:00
|
|
|
|
|
|
|
wssv.Start ();
|
2014-01-01 20:43:18 +08:00
|
|
|
if (wssv.IsListening) {
|
2014-10-08 09:38:00 +08:00
|
|
|
Console.WriteLine ("Listening on port {0}, and providing WebSocket services:", wssv.Port);
|
2014-02-13 13:38:13 +08:00
|
|
|
foreach (var path in wssv.WebSocketServices.Paths)
|
2014-03-14 19:56:14 +08:00
|
|
|
Console.WriteLine ("- {0}", path);
|
2013-10-14 19:38:15 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|