Esempio n. 1
0
        public async Task Invoke(HttpContext context)
        {
            if (!context.WebSockets.IsWebSocketRequest)
            {
                return;
            }

            UserOnlineContext objUserOnlineContext = (UserOnlineContext)context.Items["ONLINEOBJ"];

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

            _objWebSocketHandler.OnConnected(socket, objUserOnlineContext.UserLoginId);

            await Receive(socket, objUserOnlineContext, async (result, buffer, userContext) =>
            {
                if (result.MessageType == WebSocketMessageType.Text)
                {
                    await _objWebSocketHandler.ReceiveAsync(socket, result, buffer, objUserOnlineContext);
                    return;
                }
                else if (result.MessageType == WebSocketMessageType.Close)
                {
                    await _objWebSocketHandler.OnDisconnectedAsync(socket, objUserOnlineContext);
                    return;
                }
            });
        }
Esempio n. 2
0
        private async Task Receive(WebSocket socket, UserOnlineContext objUserOnlineContext, Action <WebSocketReceiveResult, byte[], UserOnlineContext> handleMessage)
        {
            var buffer = new byte[1024 * 4];

            while (socket.State == WebSocketState.Open)
            {
                var result = await socket.ReceiveAsync(buffer : new ArraySegment <byte>(buffer), cancellationToken : CancellationToken.None);

                handleMessage(result, buffer, objUserOnlineContext);
            }
        }
Esempio n. 3
0
        private void AttachUserToContext(HttpContext context, ISecurityService objSecurittyService, string SecurityToken)
        {
            try
            {
                var tokenHandler = new JwtSecurityTokenHandler();
                var key          = Encoding.ASCII.GetBytes(_objAppsetings.SecurityKey);

                var objVal = tokenHandler.ValidateToken(SecurityToken, new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key),
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ClockSkew = TimeSpan.Zero
                }, out SecurityToken validatedToken);

                context.User = objVal;

                var jwtSecurityToken = (JwtSecurityToken)validatedToken;
                var onlineid         = jwtSecurityToken.Claims.First(x => x.Type == "id").Value;

                UserOnlineContext objUserOnlineContext = new UserOnlineContext()
                {
                    OnlineId    = Convert.ToInt32(jwtSecurityToken.Claims.First(x => x.Type == "id").Value),
                    UserLoginId = jwtSecurityToken.Claims.First(x => x.Type == "loginid").Value,
                    RoleName    = jwtSecurityToken.Claims.First(x => x.Type == "role").Value,
                    UserName    = jwtSecurityToken.Claims.First(x => x.Type == "username").Value,
                };

                //retrive session object and assign to the item
                context.Items["ONLINEOBJ"] = objUserOnlineContext;
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }
Esempio n. 4
0
        public override async Task ReceiveAsync(WebSocket objWebSocket, WebSocketReceiveResult result, byte[] buffer, UserOnlineContext objUserOnlineContext)
        {
            var socketId = objWebSocketConnectionManager.GetSocketId(objWebSocket);
            var message  = Encoding.UTF8.GetString(buffer, 0, result.Count);

            Debug.Write("message received from: " + socketId + ", Message: " + message);

            MessageQueueSocketModel objMQSocketModel = new MessageQueueSocketModel()
            {
                SocketResult         = result,
                Message              = message,
                objWebSocket         = objWebSocket,
                objUserOnlineContext = objUserOnlineContext
            };

            objMQManager.EnqueueMessage(objMQSocketModel);
        }
 public virtual async Task OnDisconnectedAsync(WebSocket objWebSocket, UserOnlineContext objUserOnlineContext)
 {
     await objWebSocketConnectionManager.RemoveSocket(objWebSocketConnectionManager.GetSocketId(objWebSocket));
 }
 public abstract Task ReceiveAsync(WebSocket objWebSocket, WebSocketReceiveResult result, byte[] buffer, UserOnlineContext objUserOnlineContext);