Refactored Example3
This commit is contained in:
parent
2766978f75
commit
54be7d0168
@ -27,7 +27,7 @@ namespace Example3
|
|||||||
return Context.QueryString ["name"] ?? (_prefix + getNum ());
|
return Context.QueryString ["name"] ?? (_prefix + getNum ());
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getNum ()
|
private static int getNum ()
|
||||||
{
|
{
|
||||||
return Interlocked.Increment (ref _num);
|
return Interlocked.Increment (ref _num);
|
||||||
}
|
}
|
||||||
|
@ -8,9 +8,9 @@ namespace Example3
|
|||||||
{
|
{
|
||||||
protected override void OnMessage (MessageEventArgs e)
|
protected override void OnMessage (MessageEventArgs e)
|
||||||
{
|
{
|
||||||
var name = Context.QueryString ["name"];
|
var name = Context.QueryString ["name"] ?? String.Empty;
|
||||||
var msg = name != null
|
var msg = name.Length > 0
|
||||||
? String.Format ("Returns '{0}' to {1}", e.Data, name)
|
? String.Format ("'{0}' to {1}", e.Data, name)
|
||||||
: e.Data;
|
: e.Data;
|
||||||
|
|
||||||
Send (msg);
|
Send (msg);
|
||||||
|
@ -14,15 +14,22 @@ 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);
|
//_httpsv = new HttpServer (4649, true) // Secure;
|
||||||
#if DEBUG
|
|
||||||
|
#if DEBUG
|
||||||
_httpsv.Log.Level = LogLevel.TRACE;
|
_httpsv.Log.Level = LogLevel.TRACE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
_httpsv.RootPath = ConfigurationManager.AppSettings ["RootPath"];
|
||||||
|
|
||||||
// HTTP Basic/Digest Authentication
|
/* Secure Connection
|
||||||
/*
|
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
||||||
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Digest;
|
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
||||||
|
_httpsv.Certificate = new X509Certificate2 (cert, password);
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* HTTP Authentication (Basic/Digest)
|
||||||
|
_httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
|
||||||
_httpsv.Realm = "WebSocket Test";
|
_httpsv.Realm = "WebSocket Test";
|
||||||
_httpsv.UserCredentialsFinder = identity => {
|
_httpsv.UserCredentialsFinder = identity => {
|
||||||
var name = identity.Name;
|
var name = identity.Name;
|
||||||
@ -32,25 +39,18 @@ namespace Example3
|
|||||||
};
|
};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Secure Connection
|
|
||||||
/*
|
|
||||||
var cert = ConfigurationManager.AppSettings ["ServerCertFile"];
|
|
||||||
var password = ConfigurationManager.AppSettings ["CertFilePassword"];
|
|
||||||
_httpsv.Certificate = new X509Certificate2 (cert, password);
|
|
||||||
*/
|
|
||||||
|
|
||||||
//_httpsv.KeepClean = false;
|
//_httpsv.KeepClean = false;
|
||||||
|
|
||||||
|
_httpsv.OnGet += (sender, e) => onGet (e);
|
||||||
|
|
||||||
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
_httpsv.AddWebSocketService<Echo> ("/Echo");
|
||||||
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
_httpsv.AddWebSocketService<Chat> ("/Chat");
|
||||||
//_httpsv.AddWebSocketService<Chat> ("/Chat", () => new Chat ("Anon#"));
|
//_httpsv.AddWebSocketService<Chat> ("/Chat", () => new Chat ("Anon#"));
|
||||||
|
|
||||||
_httpsv.OnGet += (sender, e) => onGet (e);
|
|
||||||
|
|
||||||
_httpsv.Start ();
|
_httpsv.Start ();
|
||||||
if (_httpsv.IsListening) {
|
if (_httpsv.IsListening) {
|
||||||
Console.WriteLine (
|
Console.WriteLine (
|
||||||
"An HTTP Server listening on port: {0} WebSocket service paths:",
|
"An HTTP server listening on port: {0} WebSocket service paths:",
|
||||||
_httpsv.Port);
|
_httpsv.Port);
|
||||||
|
|
||||||
foreach (var path in _httpsv.WebSocketServices.ServicePaths)
|
foreach (var path in _httpsv.WebSocketServices.ServicePaths)
|
||||||
@ -73,15 +73,15 @@ namespace Example3
|
|||||||
|
|
||||||
private static void onGet (HttpRequestEventArgs eventArgs)
|
private static void onGet (HttpRequestEventArgs eventArgs)
|
||||||
{
|
{
|
||||||
var request = eventArgs.Request;
|
var req = eventArgs.Request;
|
||||||
var response = eventArgs.Response;
|
var res = eventArgs.Response;
|
||||||
var content = getContent (request.RawUrl);
|
var content = getContent (req.RawUrl);
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
response.WriteContent (content);
|
res.WriteContent (content);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
response.StatusCode = (int) HttpStatusCode.NotFound;
|
res.StatusCode = (int) HttpStatusCode.NotFound;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user