Exemple #1
0
        /// <summary>
        /// Start accepting new connections.
        /// </summary>
        /// <param name="token">Cancellation token useful for canceling the server.</param>
        public void Start(CancellationToken token = default)
        {
            if (_HttpListener != null && _HttpListener.IsListening)
            {
                throw new InvalidOperationException("WatsonWebserver is already listening.");
            }

            LoadRoutes();

            Statistics = new WatsonWebserverStatistics();

            _TokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
            _Token       = token;

            _AcceptConnections = Task.Run(() => AcceptConnections(_Token), _Token);
        }
        /// <summary>
        /// Start accepting new connections.
        /// </summary>
        /// <param name="token">Cancellation token useful for canceling the server.</param>
        /// <returns>Task.</returns>
        public Task StartAsync(CancellationToken token = default)
        {
            if (_HttpListener != null && _HttpListener.IsListening)
            {
                throw new InvalidOperationException("WatsonWebserver is already listening.");
            }

            LoadRoutes();
            Statistics = new WatsonWebserverStatistics();

            _TokenSource = CancellationTokenSource.CreateLinkedTokenSource(token);
            _Token       = token;

            foreach (string prefix in _Settings.Prefixes)
            {
                _HttpListener.Prefixes.Add(prefix);
            }

            _HttpListener.Start();
            _AcceptConnections = Task.Run(() => AcceptConnections(_Token), _Token);
            return(_AcceptConnections);
        }