websocket-sharp/Example3/Program.cs

125 lines
3.9 KiB
C#
Raw Normal View History

2012-09-10 00:36:22 +08:00
using System;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
2014-10-04 13:50:11 +08:00
using System.Text;
2012-09-10 00:36:22 +08:00
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
namespace Example3
{
public class Program
{
2014-10-04 13:50:11 +08:00
public static void Main (string[] args)
2012-09-10 00:36:22 +08:00
{
2014-10-04 13:50:11 +08:00
/* Create a new instance of the HttpServer class.
*
* If you would like to provide the secure connection, you should create the instance
* with the 'secure' parameter set to true.
*/
var httpsv = new HttpServer (4649);
//httpsv = new HttpServer (4649, true);
2014-01-04 02:15:21 +08:00
#if DEBUG
2014-10-04 13:50:11 +08:00
// To change the logging level.
httpsv.Log.Level = LogLevel.Trace;
// To change the wait time for the response to the WebSocket Ping or Close.
httpsv.WaitTime = TimeSpan.FromSeconds (2);
2014-01-04 02:15:21 +08:00
#endif
2014-10-04 13:50:11 +08:00
/* To provide the secure connection.
var cert = ConfigurationManager.AppSettings["ServerCertFile"];
var passwd = ConfigurationManager.AppSettings["CertFilePassword"];
httpsv.SslConfiguration.ServerCertificate = new X509Certificate2 (cert, passwd);
2014-01-04 02:15:21 +08:00
*/
2014-10-04 13:50:11 +08:00
/* To provide the HTTP Authentication (Basic/Digest).
httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
httpsv.Realm = "WebSocket Test";
2014-10-08 12:34:46 +08:00
httpsv.UserCredentialsFinder = id => {
2014-11-19 09:44:17 +08:00
var name = id.Name;
// Return user name, password, and roles.
return name == "nobita"
? new NetworkCredential (name, "password", "gunfighter")
2014-10-08 12:34:46 +08:00
: null; // If the user credentials aren't found.
};
*/
2014-10-04 13:50:11 +08:00
// To set the document root path.
httpsv.RootPath = ConfigurationManager.AppSettings["RootPath"];
// To set the HTTP GET method event.
httpsv.OnGet += (sender, e) => {
var req = e.Request;
var res = e.Response;
var path = req.RawUrl;
if (path == "/")
path += "index.html";
2014-10-04 13:50:11 +08:00
var content = httpsv.GetFile (path);
if (content == null) {
res.StatusCode = (int) HttpStatusCode.NotFound;
return;
}
2014-01-04 03:09:23 +08:00
2014-10-04 13:50:11 +08:00
if (path.EndsWith (".html")) {
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
2014-01-04 02:15:21 +08:00
2014-10-04 13:50:11 +08:00
res.WriteContent (content);
};
// Not to remove the inactive WebSocket sessions periodically.
//httpsv.KeepClean = false;
// To resolve to wait for socket in TIME_WAIT state.
//httpsv.ReuseAddress = true;
2014-10-04 13:50:11 +08:00
// Add the WebSocket services.
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");
/* Add the WebSocket service with initializing.
httpsv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat ("Anon#") {
Protocol = "chat",
2014-10-04 13:50:11 +08:00
// To validate the Origin header.
2014-10-08 12:34:46 +08:00
OriginValidator = val => {
// Check the value of the Origin header, and return true if valid.
Uri origin;
2014-10-08 12:34:46 +08:00
return !val.IsNullOrEmpty () &&
Uri.TryCreate (val, UriKind.Absolute, out origin) &&
origin.Host == "localhost";
},
2014-10-04 13:50:11 +08:00
// To validate the Cookies.
CookiesValidator = (req, res) => {
2014-10-08 12:34:46 +08:00
// Check the Cookies in 'req', and set the Cookies to send to the client with 'res'
// if necessary.
foreach (Cookie cookie in req) {
cookie.Expired = true;
res.Add (cookie);
}
2014-10-08 12:34:46 +08:00
return true; // If valid.
}
});
*/
2012-09-10 00:36:22 +08:00
2014-10-04 13:50:11 +08:00
httpsv.Start ();
if (httpsv.IsListening) {
2014-10-08 12:34:46 +08:00
Console.WriteLine ("Listening on port {0}, and providing WebSocket services:", httpsv.Port);
2014-10-04 13:50:11 +08:00
foreach (var path in httpsv.WebSocketServices.Paths)
2014-03-14 20:39:17 +08:00
Console.WriteLine ("- {0}", path);
}
2012-09-10 00:36:22 +08:00
Console.WriteLine ("\nPress Enter key to stop the server...");
Console.ReadLine ();
2012-09-10 00:36:22 +08:00
2014-10-04 13:50:11 +08:00
httpsv.Stop ();
2012-09-10 00:36:22 +08:00
}
}
}