public virtual async Task InvokeAsync(HttpContext context)
        {
            try
            {
                if (!context.WebSockets.IsWebSocketRequest)
                {
                    return;
                }

                var userId   = context.Request.GetHeaderValue("UserId");
                var userName = context.Request.GetHeaderValue("UserName");
                ConnectionAborted abortStatus = ConnectionAborted.None;

                if (string.IsNullOrEmpty(userId.ToString()))
                {
                    abortStatus = ConnectionAborted.UserIdNotFound;
                }
                else if (string.IsNullOrEmpty(userName))
                {
                    abortStatus = ConnectionAborted.UserNameNotFound;
                }

                var info = new WSUserInfo()
                {
                    Id   = userId,
                    Name = userName
                };

                await InvokeWSAsync(context, info, abortStatus);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "An error on opening connection of WebSocket to the server");
            }
        }
        protected async Task InvokeWSAsync(HttpContext context, WSUserInfo userInfo, ConnectionAborted abortStatus = ConnectionAborted.None)
        {
            try
            {
                await WebSocketHub.WSManager.RemoveWebSocketIfExists(userInfo.Id);

                var socket = await context.WebSockets.AcceptWebSocketAsync();

                if (abortStatus == ConnectionAborted.None)
                {
                    await WebSocketHub.OnConnectedAsync(socket, userInfo);

                    await Receive(socket, async (result, buffer) =>
                    {
                        if (result.MessageType == WebSocketMessageType.Binary)
                        {
                            await WebSocketHub.ReceiveMessageAsync(socket, Encoding.UTF8.GetString(buffer, 0, result.Count));
                        }
                        else
                        {
                            await WebSocketHub.OnDisconnectedAsync(userInfo.Id);
                        }
                        return;
                    });
                }
                else
                {
                    await WebSocketHub.WSManager.AbortConnection(socket, abortStatus);
                }
            }
            catch
            {
                throw;
            }
        }
コード例 #3
0
        /// <summary>
        /// To connect new created WebSocket to the WebSocketManager's sockets list
        /// </summary>
        /// <param name="socket">New created socket</param>
        /// <param name="userInfo">Current User info</param>
        public virtual async Task OnConnectedAsync(System.Net.WebSockets.WebSocket socket, WSUserInfo userInfo)
        {
            await WSManager.AddSocket(userInfo.Id, socket, userInfo);

            await SendNotificationAsync(userInfo.Id, new { }, Notification.WSConnected);
        }