Esempio n. 1
0
        public override Task OnConnectedAsync()
        {
            HubUserInfo u = GetHubUser();

            if (u == null)
            {
                HubUserInfo user = new HubUserInfo();
                user.full_name = GetIdentityUserFullName().Result;
                user.connection_ids.Add(Context.ConnectionId);
                user.user_id = Context.UserIdentifier;

                _hubUserService.HubUsers.Add(user);
            }
            else
            {
                //user exist, just add new connection id to him
                u.connection_ids.Add(Context.ConnectionId);
                //add this connection_id to all groups that is this user alreday in
                foreach (var group_name in u.groups)
                {
                    Groups.AddToGroupAsync(Context.ConnectionId, group_name);
                }
            }

            return(base.OnConnectedAsync());
        }
Esempio n. 2
0
        public async Task MessageAll(string message, string group_name)
        {
            HubUserInfo user = GetHubUser();

            if (user.groups.Contains(group_name))
            {
                await Clients.Group(group_name).SendAsync("NewMessage", user.full_name, message, group_name);
            }
        }
Esempio n. 3
0
        public async Task LeaveGroup(string group_name)
        {
            HubUserInfo user = GetHubUser();

            //just if it is alreday in that group
            if (user.groups.Contains(group_name))
            {
                user.groups.Remove(group_name);
                foreach (var id in user.connection_ids)
                {
                    await Groups.RemoveFromGroupAsync(id, group_name);
                }

                await Clients.Group(group_name).SendAsync("GroupChange", group_name, user.full_name, "leave");
            }
        }
Esempio n. 4
0
        public async Task JoinGroup(string group_name)
        {
            HubUserInfo user = GetHubUser();

            //just if alreday not in that group
            if (!user.groups.Contains(group_name))
            {
                user.groups.Add(group_name);
                foreach (var id in user.connection_ids)
                {
                    await Groups.AddToGroupAsync(id, group_name);
                }

                await Clients.Group(group_name).SendAsync("GroupChange", group_name, user.full_name, "join");
            }
        }
Esempio n. 5
0
        public override Task OnDisconnectedAsync(Exception exception)
        {
            HubUserInfo user = GetHubUser();

            if (user != null)
            {
                user.connection_ids.Remove(Context.ConnectionId);
                if (user.connection_ids.Count() == 0)
                {
                    _hubUserService.HubUsers.Remove(user);
                    foreach (string group in user.groups)
                    {
                        Clients.Group(group).SendAsync("GroupChange", group, user.full_name, "disconected from");
                    }
                }
            }
            return(base.OnDisconnectedAsync(exception));
        }