Ejemplo n.º 1
0
        public void CloseGame(string connectionId)
        {
            var user = CurrentConnections.FirstOrDefault(x => x.ConnectionId == connectionId);

            CurrentConnections.Remove(user);

            Clients.Caller.leaveGame();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Method which send video frame from one to another user
        /// </summary>
        public async Task <bool> SendVideoFrameAsync(ConversationModel conversationData, Connection initiator)
        {
            var targetConnection = CurrentConnections.FirstOrDefault(connection => connection.User.Id == conversationData.Target.Id);

            if (targetConnection == null)
            {
                return(false);
            }

            Packet videoPacket = new Packet
            {
                ActionState  = ActionStates.Video,
                Conversation = conversationData
            };

            return(await SendConversationResponseAsync(videoPacket, targetConnection, initiator));
        }
Ejemplo n.º 3
0
        public async Task SendNotificationAsync(Guid targetId, NotificationModel notification)
        {
            var target = CurrentConnections.FirstOrDefault(c => c.User.Id == targetId);

            if (target == null)
            {
                return;
            }

            Packet commandResultPacket = new Packet
            {
                ActionState  = ActionStates.Notification,
                Notification = notification
            };

            //Add notification in query
            if (!await NetHelper.SendDataAsync(target?.User?.TcpSocket, commandResultPacket))
            {
                CurrentNotifications.Add(targetId, notification);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        ///  Event which invoke when server receive new command from other world
        /// </summary>
        private async void Connection_OnReceivedCommand(Connection sender, ReceivedCommandEventsArgs e)
        {
            CommandModel commandResult = null;

            switch (e.Command.CommandType)
            {
            case CommandTypes.Authorization:

                DBHelper.TryAuthorizationUser(JObject.Parse(e.Command.Data), out commandResult);
                await SendCommandResultAsync(sender, commandResult);

                break;

            case CommandTypes.Registration:

                DBHelper.TryRegisterUser(JObject.Parse(e.Command.Data), out commandResult);
                await SendCommandResultAsync(sender, commandResult);

                break;

            case CommandTypes.UserInfo:

                var result = DBHelper.GetUserInfo(JObject.Parse(e.Command.Data));

                //Set Id to sender connection
                if (result.Item2 != Guid.Empty)
                {
                    var recentConnection = CurrentConnections.FirstOrDefault(cc => cc.User.TcpSocket == sender.User.TcpSocket);

                    if (recentConnection != null)
                    {
                        recentConnection.User.Id = result.Item2;
                    }

                    var notification = CurrentNotifications.FirstOrDefault(n => n.Key == recentConnection.User.Id).Value;

                    //Check that have user missed notification or not
                    if (notification != null)
                    {
                        await SendNotificationAsync(recentConnection.User.Id, notification);

                        CurrentNotifications.Remove(recentConnection.User.Id);
                    }
                }

                await SendCommandResultAsync(sender, result.Item1);

                break;

            case CommandTypes.Search:

                await SendCommandResultAsync(sender, DBHelper.GetSearchResults(JObject.Parse(e.Command.Data)));

                break;

            case CommandTypes.GetContacts:

                await SendCommandResultAsync(sender, DBHelper.GetContacts(JObject.Parse(e.Command.Data)));

                break;

            case CommandTypes.SendToApproveContact:

                result = DBHelper.AddNewContact(JObject.Parse(e.Command.Data));

                if (result.Item2 != Guid.Empty)
                {
                    await SendNotificationAsync(result.Item2, new NotificationModel { NotificationType = NotificationTypes.NewContact });
                }

                await SendCommandResultAsync(sender, result.Item1);

                break;

            case CommandTypes.RemoveContact:

                result = DBHelper.RemoveContact(JObject.Parse(e.Command.Data));

                if (result.Item2 != Guid.Empty)
                {
                    await SendNotificationAsync(result.Item2, new NotificationModel { NotificationType = NotificationTypes.RemoveContact });
                }

                await SendCommandResultAsync(sender, result.Item1);

                break;

            case CommandTypes.ApproveContact:

                result = DBHelper.ApproveContact(JObject.Parse(e.Command.Data));

                if (result.Item2 != Guid.Empty)
                {
                    await SendNotificationAsync(result.Item2, new NotificationModel { NotificationType = NotificationTypes.ApprovedContact });
                }

                await SendCommandResultAsync(sender, result.Item1);

                break;
            }
        }