예제 #1
0
        private void GotMessageOnConnection(object sender, MessageEventArgs e)
        {
            string msgType     = e.msg.GetType().Name;
            string textToPrint = "";

            switch (msgType)
            {
            case "IdentificationMessage":
                textToPrint = "user " + ((IdentificationMessage)e.msg).SourceUser.Name + " got connected";
                UserContext UserContextInActiveConnectionsList = ConnectedUsers.Single(uc => uc.Connection == (Connection)sender);
                UserContextInActiveConnectionsList.User = ((IdentificationMessage)e.msg).SourceUser;

                IdentificationMessage retMsg = (IdentificationMessage)e.msg;
                retMsg.Status = true;
                ((Connection)sender).SendMessage(retMsg);
                if (NewClientLoginEv != null)
                {
                    NewClientLoginEv(sender, new UserContextEventArgs(UserContextInActiveConnectionsList));
                }
                break;

            case "TextMessage":
                foreach (var item in ConnectedUsers.Where(uc => uc.Connection != (Connection)sender))
                {
                    item.Connection.SendMessage(e.msg);
                }
                break;

            default:
                break;
            }
            Logger.Print("INFO: " + textToPrint);
        }
예제 #2
0
 public async void RequestConnectedUsers()
 {
     // send to caller user
     var availableUser = ConnectedUsers.Where(x => x.IsOnHold)
                         .Select(x => new
     {
         userId       = x.UserId,
         userName     = x.UserName,
         messageCount = CurrentMessages.Count(y => y.FromUserId == x.UserId)
     });
     await Clients.Caller.SendAsync(ChatHubMethods.UpdateConnectedUsers, availableUser);
 }
예제 #3
0
        /// <summary>
        /// To be called when a new User Connects, Notifies other users that a new User is Connected
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public void Connect(string name)
        {
            var id = Context.ConnectionId;

            if (ConnectedUsers == null)
            {
                ConnectedUsers = new HashSet <iUser>();
            }
            if (ConnectedUsers.Count(x => x.ID == id) == 0)
            {
                ConnectedUsers.Add(new iUser()
                {
                    ID = id, Name = name
                });
                Clients.Caller.onConnected(id, name, ConnectedUsers.Where(x => x.ID != id));
                Clients.AllExcept(id).onNewUserConnected(id, name);
            }
        }