예제 #1
0
        public async Task SendMessageToAll(ClientIdentity clientIdentity, string message)
        {
            Console.WriteLine("---------------- SendMessageToAll ----------------");
            Console.WriteLine("Client: {0}", clientIdentity.ToString());

            await Clients.Others.SendAsync(HubConstant.ReceiveMessageMethod, clientIdentity, message);
        }
예제 #2
0
        public async Task Register(ClientIdentity clientIdentity)
        {
            Console.WriteLine("---------------- Register ----------------");
            Console.WriteLine("Client: {0}", clientIdentity.ToString());

            if (!userLookup.Exists(c => c.ClientId == clientIdentity.ClientId))
            {
                foreach (var onlineClient in userLookup)
                {
                    // can create another method in ChatClient that receive List<CLientIdentity> onlines
                    // in that just need to SendAsync once
                    await Clients.Caller.SendAsync(HubConstant.NotifyStateMethod, onlineClient, State.Register);
                }
                userLookup.Add(clientIdentity);
                await Clients.Others.SendAsync(HubConstant.NotifyStateMethod, clientIdentity, State.Register);

                Console.WriteLine(clientIdentity.ToString() + " connected");
            }
        }
예제 #3
0
        public override async Task OnDisconnectedAsync(Exception e)
        {
            Console.WriteLine("---------------- OnDisconnectedAsync ----------------");
            Console.WriteLine("ClientId: {0}", Context.ConnectionId);

            string         currentId      = Context.ConnectionId;
            ClientIdentity clientIdentity = userLookup.First(c => c.ClientId == currentId);

            userLookup.Remove(clientIdentity);
            Console.WriteLine(clientIdentity.ToString() + " disconnected");
            await Clients.Others.SendAsync(
                HubConstant.NotifyStateMethod, clientIdentity, State.Disconnect);

            await base.OnDisconnectedAsync(e);
        }
예제 #4
0
        public async Task SendMessageToGroup(ClientIdentity clientIdentity,
                                             string message, string groupId)
        {
            Console.WriteLine("---------------- SendMessageToGroup ----------------");
            Console.WriteLine("Client: {0}", clientIdentity.ToString());
            Console.WriteLine("GroupId: {0}", groupId);

            if (groupLookup.Exists(gr => gr.GroupId == groupId))
            {
                await Clients.OthersInGroup(groupId)
                .SendAsync(HubConstant.ReceiveMessageMethod, clientIdentity, message);
            }
            else
            {
                Console.WriteLine("GroupId [{0}] not exist", groupId);
            }
        }
예제 #5
0
        public async Task SendMessageToSpecificClient(ClientIdentity clientIdentity,
                                                      string message, string otherClientId)
        {
            Console.WriteLine("---------------- SendMessageToSpecificClient ----------------");
            Console.WriteLine("Client: {0}", clientIdentity.ToString());
            Console.WriteLine("OtherClientId: {0}", otherClientId);

            if (userLookup.Any(u => u.ClientId == otherClientId))
            {
                await Clients.Client(otherClientId)
                .SendAsync(HubConstant.ReceiveMessageMethod, clientIdentity, message);
            }
            else
            {
                Console.WriteLine("ClientId [{0}] not exist", otherClientId);
            }
        }
예제 #6
0
        public async Task CreateGroup(ClientIdentity clientIdentity, string groupName,
                                      IReadOnlyList <string> memberIdList)
        {
            Console.WriteLine("---------------- CreateGroup ----------------");
            Console.WriteLine("Client: {0}", clientIdentity.ToString());

            var groupIdentity = new GroupIdentity()
            {
                GroupId   = Guid.NewGuid().ToString("N"),
                GroupName = groupName,
                OwnerId   = clientIdentity.ClientId
            };

            foreach (var memberId in memberIdList)
            {
                await Groups.AddToGroupAsync(memberId, groupIdentity.GroupId);
            }
            await Clients.Clients(memberIdList).SendAsync(HubConstant.NotifyAddedToGroupMethod,
                                                          groupIdentity, clientIdentity);

            groupLookup.Add(groupIdentity);
        }