public bool Connect(ChatUser client)
        {
            if (!clients.ContainsValue(CurrentCallback) && !SearchClientByName(client.UserName))
            {
                lock (syncObj)
                {
                    clients.Add(client, CurrentCallback);
                    clientList.Add(client);

                    foreach (ChatUser key in clients.Keys)
                    {
                        IChatCallBack callback = clients[key];
                        try
                        {
                            callback.RefreshConnectedClient(clientList);
                            callback.UserJoin(client);
                        }
                        catch
                        {
                            clients.Remove(key);
                            return(false);
                        }
                    }
                }
                return(true);
            }
            return(false);
        }
Beispiel #2
0
 public void SendMessageDirectly(IChatCallBack callback, string msg)
 {
     _users.ForEach(delegate(Client currentClient)
     {
         if (currentClient.Callback == callback)
         {
             currentClient.Callback.GetMessage(msg);
         }
     });
 }
Beispiel #3
0
 /// <summary>
 /// Use this method if you can't send IChatCallBack
 /// </summary>
 private void UnLogginUser(IChatCallBack callback)
 {
     for (int i = 0; i < _users.Count; i++)
     {
         if (_users[i].Callback == callback)
         {
             // Makes user offline now.
             _users[i].Online = false;
         }
     }
 }
Beispiel #4
0
 /// <summary>
 /// Returns true if we haven't this callback(context) in the main dictionary.
 /// </summary>
 /// <param name="callback"></param>
 /// <returns></returns>
 private bool IsCallBackUnic(IChatCallBack callback)
 {
     for (int i = 0; i < _users.Count; i++)
     {
         if (_users[i].Callback == callback && _users[i].Online)
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #5
0
        public void Whisper(User toUser, string message)
        {
            var  connection = OperationContext.Current.GetCallbackChannel <IChatCallBack>();
            User user       = null;

            if (!_users.TryGetValue(connection, out user))
            {
            }
            IChatCallBack toUserConnection = null;

            toUserConnection = _users.Where(x => x.Value == toUser).FirstOrDefault().Key;
            if (toUserConnection != null)
            {
                toUserConnection.ReceiveWhisper(user, message);
            }
        }
Beispiel #6
0
        public void Join(User user)
        {
            IChatCallBack chatCallBack = OperationContext.Current.GetCallbackChannel <IChatCallBack>();

            //отправляем всем сообщение что мы зашли в чат
            foreach (var otherUser in _users.Keys)
            {
                otherUser.UserJoined(user);
            }
            _users.Add(chatCallBack, user);
            chatCallBack.GetUserList(_users.Select(p => p.Value).ToList());
            //foreach (var otherUser in _users.Keys)
            //{
            //    otherUser.GetUserList(_users.Select(p => p.Value).ToList());
            //}
        }
Beispiel #7
0
        public void Leave(User user)
        {
            IChatCallBack key = null;

            key = OperationContext.Current.GetCallbackChannel <IChatCallBack>();
            Console.WriteLine($"User {user.UserName} leave");
            //Console.WriteLine($"{user.UserName} key = {key}");

            if (key != null)
            {
                _users.Remove(key);
                foreach (var otherUser in _users.Keys)
                {
                    otherUser.UserLeave(user);
                }
            }
        }
Beispiel #8
0
        public void JoinChatWhithGroup(int groupId, int chatId)
        {
            List <Profile> profiles = chatController.JoinChatWithGroup(groupId, chatId);

            if (profiles.Count > 0)
            {
                foreach (Profile user in profiles)
                {
                    try
                    {
                        IChatCallBack chatCallback = (IChatCallBack)user.CallBack;
                        chatCallback.JoinChat(chatId);
                    }
                    catch (Exception)
                    {
                        Offline(user.ProfileID);
                    }
                }
            }
        }
Beispiel #9
0
        public string[] Join(string name)
        {
            string[] list = new string[chatChannelList.Count];
            try
            {
                Trace.WriteLine("User " + name + " join the chat room: start");
                username = name;
                callback = OperationContext.Current.GetCallbackChannel <IChatCallBack>();
                ChatEventArgs e = new ChatEventArgs();
                e.name    = name;
                e.msgType = MessageType.UserEnter;
                BroadcastMessage(e);
                broadcastList += MessageHandling;

                lock (obj)
                {
                    chatChannelList.Keys.CopyTo(list, 0);
                }

                myHandler = new ChatEventHandler(MessageHandling);
                lock (obj)
                {
                    if (!chatChannelList.Keys.Contains(name))
                    {
                        chatChannelList.Add(name, myHandler);
                    }
                }
                Trace.WriteLine("User " + username + " join the chat room: end");

                Console.WriteLine("Callback instance number: " + chatChannelList.Count);
                Console.WriteLine("broadcastList Delegete number: " + broadcastList.GetInvocationList().Count());
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie join method exception: " + ex.Message);
            }

            return(list);
        }
Beispiel #10
0
        public string[] Join(string name)
        {
            string[] list = new string[chatChannelList.Count];
            try
            {
                Trace.WriteLine("User " + name + " join the chat room: start");
                username = name;
                callback = OperationContext.Current.GetCallbackChannel<IChatCallBack>();
                ChatEventArgs e = new ChatEventArgs();
                e.name = name;
                e.msgType = MessageType.UserEnter;
                BroadcastMessage(e);
                broadcastList += MessageHandling;

                lock (obj)
                {
                    chatChannelList.Keys.CopyTo(list, 0);
                }

                myHandler = new ChatEventHandler(MessageHandling);
                lock (obj)
                {
                    if (!chatChannelList.Keys.Contains(name))
                    {
                        chatChannelList.Add(name, myHandler);
                    }
                }
                Trace.WriteLine("User " + username + " join the chat room: end");

                Console.WriteLine("Callback instance number: " + chatChannelList.Count);
                Console.WriteLine("broadcastList Delegete number: " + broadcastList.GetInvocationList().Count());
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Servcie join method exception: " + ex.Message);
            }

            return list;
        }
Beispiel #11
0
        public bool Invite(int chatId, string name)
        {
            Profile user = profileController.GetUser(name);

            if (user != null)
            {
                try
                {
                    IChatCallBack chatCallback = (IChatCallBack)user.CallBack;
                    chatCallback.Notification(chatController.FindChat(chatId));
                    return(true);
                }
                catch (Exception)
                {
                    Offline(user.ProfileID);
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Beispiel #12
0
 public Client()
 {
     Callback = OperationContext.Current.GetCallbackChannel <IChatCallBack>();
     RegTime  = DateTime.Now;
 }