[Modify] Polish it

This commit is contained in:
sta 2016-07-21 17:23:19 +09:00
parent 2bc95c251f
commit 02af0789e1

View File

@ -14,8 +14,8 @@ namespace Example3
{ {
// Create a new instance of the HttpServer class. // Create a new instance of the HttpServer class.
// //
// If you would like to provide the secure connection, you should create the instance with // If you would like to provide the secure connection, you should create a new instance with
// the 'secure' parameter set to true, or the https scheme HTTP URL. // the 'secure' parameter set to true, or an https scheme HTTP URL.
var httpsv = new HttpServer (4649); var httpsv = new HttpServer (4649);
//var httpsv = new HttpServer (5963, true); //var httpsv = new HttpServer (5963, true);
@ -51,7 +51,13 @@ namespace Example3
httpsv.Log.Level = LogLevel.Trace; httpsv.Log.Level = LogLevel.Trace;
// To change the wait time for the response to the WebSocket Ping or Close. // To change the wait time for the response to the WebSocket Ping or Close.
httpsv.WaitTime = TimeSpan.FromSeconds (2); //httpsv.WaitTime = TimeSpan.FromSeconds (2);
// Not to remove the inactive WebSocket sessions periodically.
//httpsv.KeepClean = false;
// To resolve to wait for socket in TIME_WAIT state.
//httpsv.ReuseAddress = true;
#endif #endif
/* To provide the secure connection. /* To provide the secure connection.
var cert = ConfigurationManager.AppSettings["ServerCertFile"]; var cert = ConfigurationManager.AppSettings["ServerCertFile"];
@ -63,13 +69,13 @@ namespace Example3
httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic; httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
httpsv.Realm = "WebSocket Test"; httpsv.Realm = "WebSocket Test";
httpsv.UserCredentialsFinder = id => { httpsv.UserCredentialsFinder = id => {
var name = id.Name; var name = id.Name;
// Return user name, password, and roles. // Return user name, password, and roles.
return name == "nobita" return name == "nobita"
? new NetworkCredential (name, "password", "gunfighter") ? new NetworkCredential (name, "password", "gunfighter")
: null; // If the user credentials aren't found. : null; // If the user credentials aren't found.
}; };
*/ */
// Set the document root path. // Set the document root path.
@ -77,36 +83,30 @@ namespace Example3
// Set the HTTP GET request event. // Set the HTTP GET request event.
httpsv.OnGet += (sender, e) => { httpsv.OnGet += (sender, e) => {
var req = e.Request; var req = e.Request;
var res = e.Response; var res = e.Response;
var path = req.RawUrl; var path = req.RawUrl;
if (path == "/") if (path == "/")
path += "index.html"; path += "index.html";
var content = httpsv.GetFile (path); var content = httpsv.GetFile (path);
if (content == null) { if (content == null) {
res.StatusCode = (int) HttpStatusCode.NotFound; res.StatusCode = (int) HttpStatusCode.NotFound;
return; return;
} }
if (path.EndsWith (".html")) { if (path.EndsWith (".html")) {
res.ContentType = "text/html"; res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8; res.ContentEncoding = Encoding.UTF8;
} }
else if (path.EndsWith (".js")) { else if (path.EndsWith (".js")) {
res.ContentType = "application/javascript"; res.ContentType = "application/javascript";
res.ContentEncoding = Encoding.UTF8; res.ContentEncoding = Encoding.UTF8;
} }
res.WriteContent (content); 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;
// Add the WebSocket services. // Add the WebSocket services.
httpsv.AddWebSocketService<Echo> ("/Echo"); httpsv.AddWebSocketService<Echo> ("/Echo");
@ -115,33 +115,35 @@ namespace Example3
/* Add the WebSocket service with initializing. /* Add the WebSocket service with initializing.
httpsv.AddWebSocketService<Chat> ( httpsv.AddWebSocketService<Chat> (
"/Chat", "/Chat",
() => new Chat ("Anon#") { () =>
// To send the Sec-WebSocket-Protocol header that has a subprotocol name. new Chat ("Anon#") {
Protocol = "chat", // To send the Sec-WebSocket-Protocol header that has a subprotocol name.
// To emit a WebSocket.OnMessage event when receives a ping. Protocol = "chat",
EmitOnPing = true, // To emit a WebSocket.OnMessage event when receives a ping.
// To ignore the Sec-WebSocket-Extensions header. EmitOnPing = true,
IgnoreExtensions = true, // To ignore the Sec-WebSocket-Extensions header.
// To validate the Origin header. IgnoreExtensions = true,
OriginValidator = val => { // To validate the Origin header.
// Check the value of the Origin header, and return true if valid. OriginValidator = val => {
Uri origin; // Check the value of the Origin header, and return true if valid.
return !val.IsNullOrEmpty () && Uri origin;
Uri.TryCreate (val, UriKind.Absolute, out origin) && return !val.IsNullOrEmpty ()
origin.Host == "localhost"; && Uri.TryCreate (val, UriKind.Absolute, out origin)
}, && origin.Host == "localhost";
// To validate the Cookies. },
CookiesValidator = (req, res) => { // To validate the cookies.
// Check the Cookies in 'req', and set the Cookies to send to the client with 'res' CookiesValidator = (req, res) => {
// if necessary. // Check the cookies in 'req', and set the cookies to send to
foreach (Cookie cookie in req) { // the client with 'res' if necessary.
cookie.Expired = true; foreach (Cookie cookie in req) {
res.Add (cookie); cookie.Expired = true;
} res.Add (cookie);
}
return true; // If valid. return true; // If valid.
}
} }
}); );
*/ */
httpsv.Start (); httpsv.Start ();