public void Start(IServerParams pParams, Action <IClientConnection> pClientConnectedAction) { Listener.Prefixes.Add("http://+:8080/"); new Thread(() => { Listener.Start(); Log.Info("Started Listening"); //ToDo: change while true to use a cancellationtoken that's set when disconnect/close is called try { while (true) { var objContext = Listener.GetContext(); if (objContext.Request.IsWebSocketRequest) { ProcessConnection(objContext, pClientConnectedAction); } else { objContext.Response.StatusCode = 400; objContext.Response.Close(); } } } catch (TaskCanceledException) { } catch (OperationCanceledException) { //ignore } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}, closing server..."); Stop(); } }) { IsBackground = true, Name = "WebSocket Accept Thread" }.Start(); }
public void Start(IServerParams pParams, Action <IClientConnection> pClientConnectedAction) { if (!(pParams is ServerParams objParams)) { Log.Error("Incorrect parameters for WebSocket server"); throw new Exception("Incorrect parameters for WebSocket server"); } _objParams = objParams; _objServer.ConnectionTimeoutSeconds = _objParams.ConnectionTimeoutSeconds; _objClientConnected = pClientConnectedAction; if (!String.IsNullOrEmpty(_objParams.CertPath)) { if (!System.IO.File.Exists(_objParams.CertPath)) { Log.Error($"{_objParams.CertPath} not found"); throw new System.IO.FileNotFoundException($"{_objParams.CertPath} not found"); } _objServer.Start( _objParams.IP, _objParams.Port, new X509Certificate2(new X509Certificate2(System.IO.File.ReadAllBytes(_objParams.CertPath), _objParams.CertPassword)) ); } else { _objServer.Start(_objParams.IP, _objParams.Port); } }
public void Start(IServerParams pParams, Action <IClientConnection> pClientConnectedAction) { Listener.Prefixes.Add($"http{(Params.Secure ? "s" : "")}://+:" + Params.Port + "/" + Params.Path.TrimStart('/')); Listener.Start(); Log.Info("Started Listening"); new Thread(() => { //ToDo: change while true to use a cancellationtoken that's set when disconnect/close is called try { while (true) { var objContext = Listener.GetContext(); if (objContext.Request.IsWebSocketRequest) { ProcessConnection(objContext, pClientConnectedAction); } else { objContext.Response.StatusCode = 400; objContext.Response.Close(); } } } catch (TaskCanceledException) { } catch (OperationCanceledException) { //ignore } finally { Console.WriteLine("closing server..."); Stop(); } }) { IsBackground = true, Name = "WebSocket Accept Thread" }.Start(); }
/// <summary> /// Starts the Server connection /// </summary> /// <param name="pParams">Parameters for the server</param> /// <param name="pAction">Action that will be run when a client connects</param> public void Start(IServerParams pParams, Action <ClientConnection> pAction) { _objServer.Start(pParams, c => pAction.Invoke(new ClientConnection(c))); }