websocket-sharp/Example3/Program.cs

180 lines
5.7 KiB
C#
Raw Permalink 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
{
2015-11-28 14:52:44 +08:00
// Create a new instance of the HttpServer class.
//
2016-08-08 16:50:02 +08:00
// If you would like to provide the secure connection, you should
2022-02-01 20:14:45 +08:00
// create a new instance with the 'secure' parameter set to true or
// with an https scheme HTTP URL.
2015-11-28 14:52:44 +08:00
2014-10-04 13:50:11 +08:00
var httpsv = new HttpServer (4649);
//var httpsv = new HttpServer (5963, true);
//var httpsv = new HttpServer (System.Net.IPAddress.Any, 4649);
//var httpsv = new HttpServer (System.Net.IPAddress.Any, 5963, true);
//var httpsv = new HttpServer (System.Net.IPAddress.IPv6Any, 4649);
//var httpsv = new HttpServer (System.Net.IPAddress.IPv6Any, 5963, true);
//var httpsv = new HttpServer ("http://0.0.0.0:4649");
//var httpsv = new HttpServer ("https://0.0.0.0:5963");
//var httpsv = new HttpServer ("http://[::0]:4649");
//var httpsv = new HttpServer ("https://[::0]:5963");
//var httpsv = new HttpServer (System.Net.IPAddress.Loopback, 4649);
//var httpsv = new HttpServer (System.Net.IPAddress.Loopback, 5963, true);
//var httpsv = new HttpServer (System.Net.IPAddress.IPv6Loopback, 4649);
//var httpsv = new HttpServer (System.Net.IPAddress.IPv6Loopback, 5963, true);
2015-07-17 15:58:42 +08:00
//var httpsv = new HttpServer ("http://localhost:4649");
//var httpsv = new HttpServer ("https://localhost:5963");
//var httpsv = new HttpServer ("http://127.0.0.1:4649");
//var httpsv = new HttpServer ("https://127.0.0.1:5963");
//var httpsv = new HttpServer ("http://[::1]:4649");
//var httpsv = new HttpServer ("https://[::1]:5963");
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.
2016-07-21 16:23:19 +08:00
//httpsv.WaitTime = TimeSpan.FromSeconds (2);
// Not to remove the inactive WebSocket sessions periodically.
//httpsv.KeepClean = false;
2014-01-04 02:15:21 +08:00
#endif
2016-08-08 16:50:02 +08:00
// To provide the secure connection.
/*
2014-10-04 13:50:11 +08:00
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
*/
2016-08-08 16:50:02 +08:00
// To provide the HTTP Authentication (Basic/Digest).
/*
2014-10-04 13:50:11 +08:00
httpsv.AuthenticationSchemes = AuthenticationSchemes.Basic;
httpsv.Realm = "WebSocket Test";
2014-10-08 12:34:46 +08:00
httpsv.UserCredentialsFinder = id => {
2016-07-21 16:23:19 +08:00
var name = id.Name;
2014-11-19 09:44:17 +08:00
2016-07-21 16:23:19 +08:00
// Return user name, password, and roles.
return name == "nobita"
? new NetworkCredential (name, "password", "gunfighter")
2022-01-31 20:57:51 +08:00
: null; // If the user credentials are not found.
2016-07-21 16:23:19 +08:00
};
*/
2016-08-08 16:50:02 +08:00
// To resolve to wait for socket in TIME_WAIT state.
//httpsv.ReuseAddress = true;
2015-11-28 14:52:44 +08:00
// Set the document root path.
2017-07-01 14:32:24 +08:00
httpsv.DocumentRootPath = ConfigurationManager.AppSettings["DocumentRootPath"];
2014-10-04 13:50:11 +08:00
2015-11-28 14:52:44 +08:00
// Set the HTTP GET request event.
2014-10-04 13:50:11 +08:00
httpsv.OnGet += (sender, e) => {
2016-07-21 16:23:19 +08:00
var req = e.Request;
var res = e.Response;
2014-10-04 13:50:11 +08:00
2016-07-21 16:23:19 +08:00
var path = req.RawUrl;
2022-01-30 21:22:43 +08:00
2016-07-21 16:23:19 +08:00
if (path == "/")
path += "index.html";
2017-06-23 14:39:35 +08:00
byte[] contents;
2022-01-30 21:22:43 +08:00
2017-06-23 14:39:35 +08:00
if (!e.TryReadFile (path, out contents)) {
2016-07-21 16:23:19 +08:00
res.StatusCode = (int) HttpStatusCode.NotFound;
2022-01-30 21:22:43 +08:00
2016-07-21 16:23:19 +08:00
return;
}
if (path.EndsWith (".html")) {
res.ContentType = "text/html";
res.ContentEncoding = Encoding.UTF8;
}
else if (path.EndsWith (".js")) {
res.ContentType = "application/javascript";
res.ContentEncoding = Encoding.UTF8;
}
2020-01-22 18:50:52 +08:00
res.ContentLength64 = contents.LongLength;
2022-01-30 21:22:43 +08:00
2020-01-22 18:50:52 +08:00
res.Close (contents, true);
2016-07-21 16:23:19 +08:00
};
2014-10-04 13:50:11 +08:00
// Add the WebSocket services.
httpsv.AddWebSocketService<Echo> ("/Echo");
httpsv.AddWebSocketService<Chat> ("/Chat");
2016-08-08 16:50:02 +08:00
// Add the WebSocket service with initializing.
/*
2014-10-04 13:50:11 +08:00
httpsv.AddWebSocketService<Chat> (
"/Chat",
2022-01-29 20:34:25 +08:00
s => {
s.Prefix = "Anon#";
// To send the Sec-WebSocket-Protocol header that has a subprotocol name.
s.Protocol = "chat";
// To ignore the Sec-WebSocket-Extensions header.
s.IgnoreExtensions = true;
// To emit a WebSocket.OnMessage event when receives a ping.
s.EmitOnPing = true;
// To validate the Origin header.
s.OriginValidator = val => {
// Check the value of the Origin header, and return true if valid.
Uri origin;
return !val.IsNullOrEmpty ()
&& Uri.TryCreate (val, UriKind.Absolute, out origin)
&& origin.Host == "localhost";
};
// To validate the cookies.
s.CookiesValidator = (req, res) => {
// Check the cookies in 'req', and set the cookies to send to
// the client with 'res' if necessary.
foreach (var cookie in req) {
cookie.Expired = true;
res.Add (cookie);
2016-07-21 16:23:19 +08:00
}
2022-01-29 20:34:25 +08:00
return true; // If valid.
};
}
2016-07-21 16:23:19 +08:00
);
*/
2012-09-10 00:36:22 +08:00
2014-10-04 13:50:11 +08:00
httpsv.Start ();
2022-01-30 21:16:15 +08:00
2014-10-04 13:50:11 +08:00
if (httpsv.IsListening) {
2014-10-08 12:34:46 +08:00
Console.WriteLine ("Listening on port {0}, and providing WebSocket services:", httpsv.Port);
2022-01-30 21:16:15 +08:00
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
}
}
}