Refactored HttpServer.cs
This commit is contained in:
parent
04966f221b
commit
16a3bfa1b9
@ -61,7 +61,7 @@ namespace WebSocketSharp.Server
|
|||||||
private HttpListener _listener;
|
private HttpListener _listener;
|
||||||
private Logger _logger;
|
private Logger _logger;
|
||||||
private int _port;
|
private int _port;
|
||||||
private Thread _receiveRequestThread;
|
private Thread _receiveThread;
|
||||||
private string _rootPath;
|
private string _rootPath;
|
||||||
private bool _secure;
|
private bool _secure;
|
||||||
private WebSocketServiceManager _services;
|
private WebSocketServiceManager _services;
|
||||||
@ -149,8 +149,8 @@ namespace WebSocketSharp.Server
|
|||||||
var os = Environment.OSVersion;
|
var os = Environment.OSVersion;
|
||||||
_windows = os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX;
|
_windows = os.Platform != PlatformID.Unix && os.Platform != PlatformID.MacOSX;
|
||||||
|
|
||||||
var prefix = String.Format ("http{0}://*:{1}/", _secure ? "s" : "", _port);
|
var pref = String.Format ("http{0}://*:{1}/", _secure ? "s" : "", _port);
|
||||||
_listener.Prefixes.Add (prefix);
|
_listener.Prefixes.Add (pref);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -503,7 +503,7 @@ namespace WebSocketSharp.Server
|
|||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processHttpRequest (HttpListenerContext context)
|
private void processRequest (HttpListenerContext context)
|
||||||
{
|
{
|
||||||
var method = context.Request.HttpMethod;
|
var method = context.Request.HttpMethod;
|
||||||
var evt = method == "GET"
|
var evt = method == "GET"
|
||||||
@ -534,7 +534,7 @@ namespace WebSocketSharp.Server
|
|||||||
context.Response.Close ();
|
context.Response.Close ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void processWebSocketRequest (HttpListenerWebSocketContext context)
|
private void processRequest (HttpListenerWebSocketContext context)
|
||||||
{
|
{
|
||||||
WebSocketServiceHost host;
|
WebSocketServiceHost host;
|
||||||
if (!_services.InternalTryGetServiceHost (context.RequestUri.AbsolutePath, out host)) {
|
if (!_services.InternalTryGetServiceHost (context.RequestUri.AbsolutePath, out host)) {
|
||||||
@ -554,11 +554,11 @@ namespace WebSocketSharp.Server
|
|||||||
state => {
|
state => {
|
||||||
try {
|
try {
|
||||||
if (ctx.Request.IsUpgradeTo ("websocket")) {
|
if (ctx.Request.IsUpgradeTo ("websocket")) {
|
||||||
processWebSocketRequest (ctx.AcceptWebSocket (null, _logger));
|
processRequest (ctx.AcceptWebSocket (null, _logger));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
processHttpRequest (ctx);
|
processRequest (ctx);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
_logger.Fatal (ex.ToString ());
|
_logger.Fatal (ex.ToString ());
|
||||||
@ -583,15 +583,15 @@ namespace WebSocketSharp.Server
|
|||||||
private void startReceiving ()
|
private void startReceiving ()
|
||||||
{
|
{
|
||||||
_listener.Start ();
|
_listener.Start ();
|
||||||
_receiveRequestThread = new Thread (new ThreadStart (receiveRequest));
|
_receiveThread = new Thread (new ThreadStart (receiveRequest));
|
||||||
_receiveRequestThread.IsBackground = true;
|
_receiveThread.IsBackground = true;
|
||||||
_receiveRequestThread.Start ();
|
_receiveThread.Start ();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopReceiving (int millisecondsTimeout)
|
private void stopReceiving (int millisecondsTimeout)
|
||||||
{
|
{
|
||||||
_listener.Close ();
|
_listener.Close ();
|
||||||
_receiveRequestThread.Join (millisecondsTimeout);
|
_receiveThread.Join (millisecondsTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@ -671,12 +671,12 @@ namespace WebSocketSharp.Server
|
|||||||
/// </param>
|
/// </param>
|
||||||
public byte[] GetFile (string path)
|
public byte[] GetFile (string path)
|
||||||
{
|
{
|
||||||
var filePath = RootPath + path;
|
path = RootPath + path;
|
||||||
if (_windows)
|
if (_windows)
|
||||||
filePath = filePath.Replace ("/", "\\");
|
path = path.Replace ("/", "\\");
|
||||||
|
|
||||||
return File.Exists (filePath)
|
return File.Exists (path)
|
||||||
? File.ReadAllBytes (filePath)
|
? File.ReadAllBytes (path)
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user