Exemplo n.º 1
0
        public void Start()
        {
            if (HttpListener.IsListening)
            {
                log.Info("HTTP 协议侦听器已启动");
                return;
            }
            ;

            log.Info("开始启动WebSocket服务");

            HttpListener.Prefixes.Add($"http://{Ip}:{Port}/");
            //HttpListener.Prefixes.Add($"https://{Ip}:{Port}/");

            log.Info("支持的资源标识符 (URI) 前缀:");
            foreach (var item in HttpListener.Prefixes)
            {
                log.Info(item);
            }

            if (OnWebSocketServiceBeginStart != null)
            {
                OnWebSocketServiceBeginStart.Invoke(this, HttpListener);
            }

            HttpListener.Start();

            ThreadPool.QueueUserWorkItem(new WaitCallback(p => CreateWebSocket(p as HttpListener)), HttpListener);
            if (OnWebSocketServiceStart != null)
            {
                OnWebSocketServiceStart.Invoke(this, HttpListener);
            }
            log.Info("服务已启动");
        }
Exemplo n.º 2
0
        public void Start()
        {
            if (HttpListener.IsListening)
            {
                Console.WriteLine("HTTP 协议侦听器已启动");
                return;
            }
            ;

            Console.WriteLine("开始启动WebSocket服务");

            HttpListener.Prefixes.Add($"http://{Ip}:{Port}/");
            //HttpListener.Prefixes.Add($"https://{Ip}:{Port}/");

            Console.WriteLine("支持的资源标识符 (URI) 前缀:");
            foreach (var item in HttpListener.Prefixes)
            {
                Console.WriteLine(item);
            }

            if (OnWebSocketServiceBeginStart != null)
            {
                OnWebSocketServiceBeginStart.Invoke(this, HttpListener);
            }

            HttpListener.Start();

            //ThreadPool.QueueUserWorkItem(new WaitCallback(p => CreateWebSocket(p as HttpListener)), HttpListener);
            var task = new Task(() => { CreateWebSocket(HttpListener); }, CancellationTokenSource.Token);

            task.Start();
            if (OnWebSocketServiceStart != null)
            {
                OnWebSocketServiceStart.Invoke(this, HttpListener);
            }
            Console.WriteLine("服务已启动");
        }