Beispiel #1
0
        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();
        }
Beispiel #2
0
 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();
 }