Exemple #1
0
 private void WebSocketService_OnReceiveMessage(WebSocketContext context, string message)
 {
     Console.WriteLine($"服务器接收到消息:{message}");
     context.SendMessageAsync($"服务器接收到消息:{message}").Wait();
 }
Exemple #2
0
        /// <summary>
        /// 监听端口 创建WebSocket
        /// </summary>
        /// <param name="httpListener"></param>
        private void CreateWebSocket(HttpListener httpListener)
        {
            if (httpListener == null)
            {
                throw new Exception("HttpListener未创建");
            }
            if (!httpListener.IsListening)
            {
                throw new Exception("HttpListener未启动");
            }
            HttpListenerContext listenerContext = null;

            //try
            //{
            listenerContext = httpListener.GetContext();
            //}
            //catch (Exception)
            //{
            //    //if (httpListener != null&& httpListener.IsListening)
            //        CreateWebSocket(httpListener);
            //    return;
            //}
            if (OnAuthentication != null)
            {
                foreach (OnAuthentication item in OnAuthentication.GetInvocationList())
                {
                    if (!item.Invoke(listenerContext))
                    {
                        CreateWebSocket(httpListener);
                        return;
                    }
                }
            }

            WebSocketContext webSocket = null;

            try
            {
                webSocket = new WebSocketContext(listenerContext, SubProtocol);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                CreateWebSocket(HttpListener);
                return;
            }

            Console.WriteLine($"成功创建WebSocket:{webSocket.ID}");

            int workerThreads = 0, completionPortThreads = 0;

            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            if (workerThreads <= ReservedThreadsCount + 1 || completionPortThreads <= ReservedThreadsCount + 1)
            {
                /**
                 * 可用线程小于5
                 * 通知客户端关闭连接
                 * */
                webSocket.CloseAsync(WebSocketCloseStatus.InternalServerError, "可用线程不足,无法连接").Wait();
            }
            else
            {
                if (OnReceiveMessage != null)
                {
                    webSocket.OnReceiveMessage += OnReceiveMessage;
                }
                webSocket.OnCloseWebSocket += WebSocket_OnCloseWebSocket;
                webSocketContexts.Add(webSocket);
                if (OnAddWebSocket != null)
                {
                    OnAddWebSocket.Invoke(webSocket);
                }
                ThreadPool.QueueUserWorkItem(new WaitCallback(p =>
                {
                    (p as WebSocketContext).ReceiveMessageAsync().Wait();
                }), webSocket);
            }

            CreateWebSocket(HttpListener);
        }
Exemple #3
0
 private void WebSocketService_OnRemoveWebSocket(WebSocketContext webSocketContext)
 {
     Console.WriteLine("WebSocketService_OnRemoveWebSocket");
 }