Skip to content

Commit

Permalink
improve using Uri for open HttpServer connection
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Nov 18, 2023
1 parent 8bb7986 commit 248390c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/HTTP/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class HttpServer : IHttpServer
private readonly List<(string path, Action<Request, WebSocketClient> callback)> _wsMap;

public bool IsOpen => _listener != null && _listener.IsListening;
public Host Host { get; private set; } = new Host(IPAddress.Any, 0);
public Uri Host { get; private set; } = new Uri("http://0.0.0.0:80/");

public HttpServer()
{
Expand All @@ -29,7 +29,7 @@ public HttpServer()
_wsMap = new List<(string path, Action<Request, WebSocketClient> callback)>();
}

public void Open(Host host)
public void Open(Uri host)
{
if (IsOpen || _tryClose || _tryOpen) return;
_tryOpen = true;
Expand All @@ -40,13 +40,14 @@ public void Open(Host host)
{
HttpListener server = new HttpListener();

string protocol = "http://";
string url = $"{protocol}{host.Address}:{host.Port}/";
Console.WriteLine(url);
server.Prefixes.Add(url);
string httpUrl = $"{Uri.UriSchemeHttp}{Uri.SchemeDelimiter}{host.Host}:{host.Port}/";

server.Prefixes.Add(httpUrl);

server.Start();

Host = host;

_listener = server;

_onOpen?.Invoke(null, null);
Expand Down Expand Up @@ -272,16 +273,14 @@ private void _ReceiveRequests()
{
path.callback?.Invoke(request, websocket);
}

websocket.InitWebSocketServerSide();
});
}
}
catch (Exception e)
catch
{
// Ignored
// TODO: DEBUG ERROR
Console.WriteLine(e);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/HTTP/IHttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Netly
internal interface IHttpServer
{
bool IsOpen { get; }
Host Host { get; }
Uri Host { get; }

void Open(Host host);
void Open(Uri host);
void OnOpen(Action callback);

void OnError(Action<Exception> exception);
Expand Down

0 comments on commit 248390c

Please sign in to comment.