/// <summary>
        /// 返回信息到用户
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="userWebSocket"></param>
        /// <param name="wsFactory"></param>
        /// <returns></returns>
        public async Task BroadcastOthers(byte[] buffer, string username, ICustomWebSocketFactory wsFactory)
        {
            var others = wsFactory.Others(username);

            foreach (var uws in others)
            {
                //string msg = Encoding.UTF8.GetString(buffer);
                //String JSON = msg.Replace("\u0000", "");
                //CustomWebSocketMessage message = JSON.ConvertToObject<CustomWebSocketMessage>();

                //if (message.Type == WSMessageType.发送)
                //{

                Helperlog4.Info("返回信息到用户" + uws.ConvertToJson() + "&&&&" + Encoding.UTF8.GetString(buffer));
                //string serialisedMessage = JsonConvert.SerializeObject(message);
                //buffer = Encoding.UTF8.GetBytes(serialisedMessage);
                await uws.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);

                //}
            }

            //var others = wsFactory.Client(userWebSocket.Username);

            //await others.WebSocket.SendAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);
        }
        public async Task BroadcastOthers(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
        {
            var others = wsFactory.Others(userWebSocket);

            foreach (var uws in others)
            {
                await uws.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 发送消息给除了指定客户端外的其他的客户端
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="userWebSocket"></param>
        /// <param name="wsFactory"></param>
        /// <returns></returns>
        public async Task BroadcastOthers(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
        {
            var others = wsFactory.Others(userWebSocket);

            foreach (var uws in others)
            {
                if (uws.WebSocket.State != WebSocketState.Open)
                {
                    Log4netHelper.Info(this, "此websocket的状态为:" + uws.Username + "<>" + uws.WebSocket.State.ToString());
                    continue;
                }
                await uws.WebSocket.SendAsync(new ArraySegment <byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);
            }
        }
        /// <summary>
        /// 心跳类
        /// </summary>
        public async Task Invoke(string Username, ICustomWebSocketFactory wsFactory, ICustomWebSocketMessageHandler wsmHandler)
        {
            while (true)
            {
                try
                {
                    System.Threading.Thread.Sleep(1000);

                    List <CustomWebSocket> CustomWebSocketList = new List <CustomWebSocket>();
                    CustomWebSocketList = wsFactory.Others(Username);

                    foreach (CustomWebSocket item in CustomWebSocketList)
                    {
                        if (item.WebSocket.State == WebSocketState.Aborted || item.WebSocket.State == WebSocketState.Closed)
                        {
                            wsFactory.Remove(item.Username);
                            ClientWebSocket _webSocket    = new ClientWebSocket();
                            CustomWebSocket userWebSocket = new CustomWebSocket()
                            {
                                WebSocket = _webSocket,
                                Username  = item.Username,
                                UseSid    = item.UseSid,
                                Url       = item.Url
                            };
                            wsFactory.Add(userWebSocket);
                            await wsmHandler.SendInitialMessages(userWebSocket);

                            //await Listen(context, userWebSocket, wsFactory, wsmHandler);
                        }
                    }
                }
                catch (WebSocketException e)
                {
                    // 产生 10035 == WSAEWOULDBLOCK 错误,说明被阻止了,但是还是连接的
                    //if (e.NativeErrorCode.Equals(10035))
                    //{
                    //    return true;
                    //}
                    //else
                    //{
                    //    return false;
                    //}
                }
                finally
                {
                    //socket.Blocking = blockingState;    // 恢复状态
                }
            }
        }
        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="context"></param>
        /// <param name="wsFactory"></param>
        /// <param name="wsmHandler"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context, ICustomWebSocketFactory wsFactory, ICustomWebSocketMessageHandler wsmHandler, IAutomaticPostingFactory apFactory)
        {
            if (context.Request.Path == "/MesServiceStation")
            {
                Helperlog4.Info("新连接完成" + context.Request.Path.ToString());
                if (context.WebSockets.IsWebSocketRequest)
                {
                    System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
                    string usesid           = currentUser.User.ToString();
                    string customwebsocketS = context.Request.Query["username"];
                    //string username = usesid + "------Name";
                    if (!string.IsNullOrEmpty(customwebsocketS))
                    {
                        CustomWebSocket customwebsocket = customwebsocketS.ConvertToObject <CustomWebSocket>();
                        WebSocket       webSocket       = await context.WebSockets.AcceptWebSocketAsync();

                        List <CustomWebSocket> CustomWebSocketList = new List <CustomWebSocket>();
                        CustomWebSocketList = wsFactory.Others(customwebsocket.Username);
                        if (CustomWebSocketList.Count > 0)
                        {
                            CustomWebSocket userWebSocket1 = new CustomWebSocket();
                            userWebSocket1 = CustomWebSocketList[0];
                            wsFactory.Remove(userWebSocket1.Username);
                            CustomWebSocket userWebSocket = new CustomWebSocket()
                            {
                                WebSocket = webSocket,
                                Username  = userWebSocket1.Username,
                                UseSid    = usesid,
                                Url       = context.Request.GetDisplayUrl(),
                                UserType  = userWebSocket1.UserType
                            };
                            wsFactory.Add(userWebSocket);
                            //await Heartbeat(wsFactory, wsmHandler);
                            Helperlog4.Info("重新连接返回值" + userWebSocket.ConvertToJson());
                            await wsmHandler.SendInitialMessages(userWebSocket);
                            await Listen(context, userWebSocket, wsFactory, wsmHandler, apFactory);
                        }
                        else
                        {
                            CustomWebSocket userWebSocket = new CustomWebSocket()
                            {
                                WebSocket = webSocket,
                                Username  = customwebsocket.Username,
                                UseSid    = usesid,
                                Url       = context.Request.GetDisplayUrl(),
                                UserType  = customwebsocket.UserType
                            };
                            wsFactory.Add(userWebSocket);

                            Helperlog4.Info("新连接返回值" + userWebSocket.ConvertToJson());
                            //await Heartbeat(wsFactory, wsmHandler);
                            await wsmHandler.SendInitialMessages(userWebSocket);
                            await Listen(context, userWebSocket, wsFactory, wsmHandler, apFactory);
                        }
                    }
                }
                else
                {
                    context.Response.StatusCode = 400;
                }
            }
            await _next(context);
        }