Browse Source

Modified Example3

pull/168/head
sta 12 years ago
parent
commit
3befb74114
  1. 2
      Example3/AssemblyInfo.cs
  2. 4
      Example3/Echo.cs
  3. 46
      Example3/Program.cs

2
Example3/AssemblyInfo.cs

@ -9,7 +9,7 @@ using System.Runtime.CompilerServices;
[assembly: AssemblyConfiguration("")] [assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")] [assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")] [assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("sta")]
[assembly: AssemblyCopyright("sta.blockhead")]
[assembly: AssemblyTrademark("")] [assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")] [assembly: AssemblyCulture("")]

4
Example3/Echo.cs

@ -8,8 +8,8 @@ namespace Example3
{ {
protected override void OnMessage (MessageEventArgs e) protected override void OnMessage (MessageEventArgs e)
{ {
var name = Context.QueryString ["name"] ?? String.Empty;
var msg = name.Length > 0
var name = Context.QueryString ["name"];
var msg = !name.IsNullOrEmpty ()
? String.Format ("'{0}' to {1}", e.Data, name) ? String.Format ("'{0}' to {1}", e.Data, name)
: e.Data; : e.Data;

46
Example3/Program.cs

@ -14,19 +14,18 @@ namespace Example3
public static void Main (string [] args) public static void Main (string [] args)
{ {
_httpsv = new HttpServer (4649); _httpsv = new HttpServer (4649);
//_httpsv = new HttpServer (4649, true) // Secure;
//_httpsv = new HttpServer (4649, true); // For Secure Connection
#if DEBUG #if DEBUG
// Changing logging level
_httpsv.Log.Level = LogLevel.Trace; _httpsv.Log.Level = LogLevel.Trace;
#endif #endif
/* Secure Connection
/* For Secure Connection
var cert = ConfigurationManager.AppSettings ["ServerCertFile"]; var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
var password = ConfigurationManager.AppSettings ["CertFilePassword"]; var password = ConfigurationManager.AppSettings ["CertFilePassword"];
_httpsv.Certificate = new X509Certificate2 (cert, password); _httpsv.Certificate = new X509Certificate2 (cert, password);
*/ */
/* HTTP Authentication (Basic/Digest)
/* For HTTP Authentication (Basic/Digest)
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic; _httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
_httpsv.Realm = "WebSocket Test"; _httpsv.Realm = "WebSocket Test";
_httpsv.UserCredentialsFinder = identity => { _httpsv.UserCredentialsFinder = identity => {
@ -37,46 +36,32 @@ namespace Example3
}; };
*/ */
// Not to remove inactive clients in WebSocket services periodically
//_httpsv.KeepClean = false; //_httpsv.KeepClean = false;
// Setting document root path
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"]; _httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
// Setting HTTP method events
_httpsv.OnGet += (sender, e) => onGet (e); _httpsv.OnGet += (sender, e) => onGet (e);
// Adding WebSocket services
_httpsv.AddWebSocketService<Echo> ("/Echo"); _httpsv.AddWebSocketService<Echo> ("/Echo");
_httpsv.AddWebSocketService<Chat> ("/Chat"); _httpsv.AddWebSocketService<Chat> ("/Chat");
/* With initializing /* With initializing
_httpsv.AddWebSocketService<Echo> (
"/Echo",
() => new Echo () {
Protocol = "echo",
OriginValidator = value => {
Uri origin;
return !value.IsNullOrEmpty () &&
Uri.TryCreate (value, UriKind.Absolute, out origin) &&
origin.Host == "localhost";
},
CookiesValidator = (req, res) => {
foreach (Cookie cookie in req) {
cookie.Expired = true;
res.Add (cookie);
}
return true;
}
});
_httpsv.AddWebSocketService<Chat> ( _httpsv.AddWebSocketService<Chat> (
"/Chat", "/Chat",
() => new Chat ("Anon#") { () => new Chat ("Anon#") {
Protocol = "chat", Protocol = "chat",
// Checking Origin header
OriginValidator = value => { OriginValidator = value => {
Uri origin; Uri origin;
return !value.IsNullOrEmpty () && return !value.IsNullOrEmpty () &&
Uri.TryCreate (value, UriKind.Absolute, out origin) && Uri.TryCreate (value, UriKind.Absolute, out origin) &&
origin.Host == "localhost"; origin.Host == "localhost";
}, },
// Checking Cookies
CookiesValidator = (req, res) => { CookiesValidator = (req, res) => {
foreach (Cookie cookie in req) { foreach (Cookie cookie in req) {
cookie.Expired = true; cookie.Expired = true;
@ -91,11 +76,10 @@ namespace Example3
_httpsv.Start (); _httpsv.Start ();
if (_httpsv.IsListening) { if (_httpsv.IsListening) {
Console.WriteLine ( Console.WriteLine (
"An HTTP server listening on port: {0} WebSocket service paths:",
_httpsv.Port);
"An HTTP server listening on port: {0}, providing WebSocket services:", _httpsv.Port);
foreach (var path in _httpsv.WebSocketServices.Paths) foreach (var path in _httpsv.WebSocketServices.Paths)
Console.WriteLine (" {0}", path);
Console.WriteLine ("- {0}", path);
} }
Console.WriteLine ("\nPress Enter key to stop the server..."); Console.WriteLine ("\nPress Enter key to stop the server...");
@ -112,10 +96,10 @@ namespace Example3
return _httpsv.GetFile (path); return _httpsv.GetFile (path);
} }
private static void onGet (HttpRequestEventArgs eventArgs)
private static void onGet (HttpRequestEventArgs e)
{ {
var req = eventArgs.Request;
var res = eventArgs.Response;
var req = e.Request;
var res = e.Response;
var content = getContent (req.RawUrl); var content = getContent (req.RawUrl);
if (content != null) { if (content != null) {
res.WriteContent (content); res.WriteContent (content);

Loading…
Cancel
Save