Exemplo n.º 1
0
        private ChatData.User GetOrRegisterUser(string name)
        {
            var user = ChatData.Instance.Users.FirstOrDefault(u => u.Name == name);

            if (user != null)
            {
                return(user);
            }

            // register
            else if (!string.IsNullOrWhiteSpace(name))
            {
                user = new ChatData.User()
                {
                    Name = name,
                };

                ChatData.Instance.Users.Add(user);
                ChatData.Instance.Save();

                return(user);
            }

            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public void Connect(string name, string contact)
        {
            var pair = _userConnections.FirstOrDefault(u => u.Value.Name == name);

            if (pair.Value != null)
            {
                if (!_userConnections.TryRemove(pair.Key, out ChatData.User value))
                {
                    return;
                }
            }

            var newUser = new ChatData.User()
            {
                Name         = name,
                ConnectionId = Context.ConnectionId,
            };

            if (!_userConnections.TryAdd(name, newUser))
            {
                return;
            }

            ActivateChat(name, contact);

            // Update contacts list of other clients
            Clients.Others.clearContacts();

            // add All contact, which will broadcast all messsages
            Clients.All.addContact("All", "DEP");
            Clients.Others.addContact(name, "DEP");        // front-end won't add if it is already in list

            // and update Caller contact list
            foreach (var u in _userConnections.Values)
            {
                if (u.Name != name)
                {
                    Clients.Caller.addContact(u.Name, "DEP");
                }
            }
        }