websocket-sharp/Example3/Program.cs

113 lines
3.1 KiB
C#
Raw Normal View History

2012-09-10 00:36:22 +08:00
using System;
using System.Configuration;
using System.Security.Cryptography.X509Certificates;
2012-09-10 00:36:22 +08:00
using WebSocketSharp;
using WebSocketSharp.Net;
using WebSocketSharp.Server;
namespace Example3
{
public class Program
{
private static HttpServer _httpsv;
2012-09-10 00:36:22 +08:00
public static void Main (string [] args)
2012-09-10 00:36:22 +08:00
{
_httpsv = new HttpServer (4649);
2014-03-14 20:39:17 +08:00
//_httpsv = new HttpServer (4649, true); // For Secure Connection
2014-01-04 02:15:21 +08:00
#if DEBUG
2014-04-01 14:20:50 +08:00
// Changing the logging level
_httpsv.Log.Level = LogLevel.Trace;
2014-01-04 02:15:21 +08:00
#endif
2014-03-14 20:39:17 +08:00
/* For Secure Connection
2014-01-04 02:15:21 +08:00
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
_httpsv.Certificate = new X509Certificate2 (cert, password);
*/
2014-03-14 20:39:17 +08:00
/* For HTTP Authentication (Basic/Digest)
2014-01-04 02:15:21 +08:00
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
_httpsv.Realm = "WebSocket Test";
_httpsv.UserCredentialsFinder = identity => {
2014-01-27 14:39:27 +08:00
var expected = "nobita";
return identity.Name == expected
? new NetworkCredential (expected, "password", "gunfighter")
: null;
};
*/
2014-03-14 20:39:17 +08:00
// Not to remove inactive clients in WebSocket services periodically
//_httpsv.KeepClean = false;
2014-04-01 14:20:50 +08:00
// Setting the document root path
2014-01-04 03:09:23 +08:00
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
2014-03-14 20:39:17 +08:00
// Setting HTTP method events
2014-01-04 02:15:21 +08:00
_httpsv.OnGet += (sender, e) => onGet (e);
2014-03-14 20:39:17 +08:00
// Adding WebSocket services
_httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat");
/* With initializing
_httpsv.AddWebSocketService<Chat> (
"/Chat",
() => new Chat ("Anon#") {
Protocol = "chat",
2014-03-14 20:39:17 +08:00
// Checking Origin header
OriginValidator = value => {
Uri origin;
return !value.IsNullOrEmpty () &&
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
origin.Host == "localhost";
},
2014-03-14 20:39:17 +08:00
// Checking Cookies
CookiesValidator = (req, res) => {
foreach (Cookie cookie in req) {
cookie.Expired = true;
res.Add (cookie);
}
return true;
}
});
*/
2012-09-10 00:36:22 +08:00
_httpsv.Start ();
if (_httpsv.IsListening) {
Console.WriteLine (
2014-03-14 20:39:17 +08:00
"An HTTP server listening on port: {0}, providing WebSocket services:", _httpsv.Port);
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-01-04 02:15:21 +08:00
_httpsv.Stop ();
2012-09-10 00:36:22 +08:00
}
private static byte [] getContent (string path)
2012-09-10 00:36:22 +08:00
{
if (path == "/")
path += "index.html";
return _httpsv.GetFile (path);
2012-09-10 00:36:22 +08:00
}
2014-03-14 20:39:17 +08:00
private static void onGet (HttpRequestEventArgs e)
2012-09-10 00:36:22 +08:00
{
2014-03-14 20:39:17 +08:00
var req = e.Request;
var res = e.Response;
2014-01-04 02:15:21 +08:00
var content = getContent (req.RawUrl);
if (content != null) {
2014-01-04 02:15:21 +08:00
res.WriteContent (content);
2012-09-10 00:36:22 +08:00
return;
}
2014-01-04 02:15:21 +08:00
res.StatusCode = (int) HttpStatusCode.NotFound;
2012-09-10 00:36:22 +08:00
}
}
}