Esempio n. 1
0
        public bool Connect(Client client)
        {
            if (!_clients.ContainsKey(client) && !SearchClientsByName(client.Name))
            {

                _clients.TryAdd(client, CurrentCallback);
                _clientList.Add(client);

                foreach (Client key in _clients.Keys)
                {
                    IChatCallback callback = _clients[key];
                    try
                    {
                        callback.RefreshClients(_clientList);
                        callback.UserJoin(client);
                    }
                    catch
                    {
                        _clients.TryRemove(key, out callback);
                        return false;
                    }

                }
                return true;
            }
            return false;
        }
Esempio n. 2
0
        public void Whisper(Message msg, Client receiver)
        {

            foreach (Client rec in _clients.Keys)
            {
                if (rec.Name == receiver.Name)
                {
                    IChatCallback callback = _clients[rec];
                    callback.ReceiveWhisper(msg, rec);

                    foreach (Client sender in _clients.Keys)
                    {
                        if (sender.Name == msg.Sender)
                        {
                            IChatCallback senderCallback = _clients[sender];
                            senderCallback.ReceiveWhisper(msg, rec);
                            return;
                        }
                    }
                }
            }

        }
Esempio n. 3
0
 /// <summary>
 /// Saves the selected online client, for usage when whispering
 /// </summary>
 public void chatListBoxNames_SelectionChanged()
 {
     //If user select an online client, make a client object
     //to be the receiver if the user wants to whisper him.
     ListBoxItem item = _viewModel.ChatNameSelected;
     if (item != null)
     {
         _receiver = _onlineClients[item];
     }
 }
Esempio n. 4
0
        /// <summary>
        /// This is the second important method, which creates 
        /// the proxy, subscribe to connection state events
        /// and open a connection with the service
        /// </summary>
        public void Connect()
        {
            if (_channel == null)
            {
                try
                {
                    _localClient = new SharedComponents.Client
                    {
                        Name = _viewModel.Name,
                        AvatarID = _viewModel.SelectedIndex
                    };
                    InstanceContext context = new InstanceContext(this);

                    //As the address in the configuration file is set to localhost
                    //we want to change it so we can call a service in internal 
                    //network, or over internet
                    string servicePath = "/WPFHost/tcp";
                    string[] address = _viewModel.ConnectionIP.Split(':');
                    var binding = new NetTcpBinding("NetTcpBinding");
                    var endAdd = new EndpointAddress("net.tcp://" + address[0] + ":" + address[1] + servicePath);
                    _channel = new WcfChatClient(context, binding, endAdd);
                    _channel.Open();
                    _channel.InnerDuplexChannel.Faulted += InnerDuplexChannel_Faulted;
                    _channel.InnerDuplexChannel.Opened += InnerDuplexChannel_Opened;
                    _channel.InnerDuplexChannel.Closed += InnerDuplexChannel_Closed;
                    _channel.SetProxy(_channel.ChannelFactory.CreateChannel());
                    _channel.ConnectCompleted += proxy_ConnectCompleted;
                    //_channel.Connect(_localClient);
                    //_channel.ConnectCompleted += (proxy_ConnectCompleted);
                    _messages.Clear();
                    _msgIds.Clear();
                    //load all previous messages from database
                    _channel.ConnectAsync(_localClient);
                    //proxy_ConnectCompleted(output);
                    using (var temp = new ChatDataContainer())
                    {
                        var user = temp.ChatUsersSet.Where(s => s.Username == _viewModel.Name).ToList();
                        if (user.Count == 0)
                        {
                            var newuser = new ChatUsers
                            {
                                AvatarID = _viewModel.SelectedIndex,
                                Username = _viewModel.Name
                            };
                            temp.ChatUsersSet.Add(newuser);
                            temp.SaveChanges();
                        }
                        var cur = temp.ChatUsersSet.SingleOrDefault(t => t.Username == _viewModel.Name);
                        if (cur != null)
                        {
                            long id = cur.Id;
                            var userlist = temp.ChatUsersSet.Select(s => new { ID = s.Id, Avatar = s.AvatarID, Name = s.Username });
                            foreach (var row in temp.ChatDataSet.Where(s => s.Id == id))
                            {
                                var currentUser = userlist.Where(s => s.ID == row.Id).ToList();
                                ListBoxItem item = MakeItem(currentUser[0].Avatar, currentUser[0].Name + " : " + row.Message);
                                _viewModel.ChatMsgs.Add(item);
                                _messages.Add(row.Message);
                                _msgIds.Add(currentUser[0].Name);
                                _viewModel.ScrollDown();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _viewModel.Name = ex.Message;
                    _viewModel.ChatStatusLabel = "Offline";
                    _viewModel.ConnectButtonIsEnabled = true;
                }
            }
            else
            {
                HandleProxy();
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Deletes file from service and sends confirmation to client.
 /// </summary>
 /// <param name="client"></param>
 /// <param name="filename"></param>
 public void FinaliseFileTransfer(Client client,string filename)
 {
     File.Delete(filename);
     foreach (Client c in _clients.Keys.Where(c => client.Name == c.Name))
     {
         IChatCallback temp;
         _clients.TryGetValue(c, out temp);
         temp?.FileSendingDone();
     }
 }
Esempio n. 6
0
 /// <summary>
 /// Tells client to establish connection for file streaming.
 /// </summary>
 /// <param name="sender">Client that sent the file.</param>
 /// <param name="receiver">Client that will receive the file.</param>
 /// <param name="filename">File that the client will receive.</param>
 public void SendFileToClient(Client sender,Client receiver,string filename)
 {
     foreach (Client c in _clients.Keys.Where(c => receiver.Name == c.Name))
     {
         IChatCallback temp;
         _clients.TryGetValue(c,out temp);
         temp?.PrepareForFile(sender, filename);
     }
 }
Esempio n. 7
0
 public void Disconnect(Client client)
 {
     foreach (Client c in _clients.Keys.Where(c => client.Name == c.Name))
     {
         IChatCallback temp;
         _clients.TryRemove(c, out temp);
         _clientList.Remove(c);
         foreach (IChatCallback callback in _clients.Values)
         {
             callback.RefreshClients(_clientList);
             callback.UserLeave(client);
         }
         return;
     }
 }
Esempio n. 8
0
       /* public bool SendFile(FileMessage fileMsg, Client receiver)
        {
            foreach (Client rcvr in _clients.Keys)
            {
                if (rcvr.Name == receiver.Name)
                {
                    Message msg = new Message
                    {
                        Sender = fileMsg.Sender,
                        Content = "I'M SENDING FILE.. " + fileMsg.FileName
                    };

                    IChatCallback rcvrCallback = _clients[rcvr];
                    rcvrCallback.ReceiveWhisper(msg, receiver);
                    rcvrCallback.ReceiverFile(fileMsg, receiver);

                    foreach (Client sender in _clients.Keys)
                    {
                        if (sender.Name == fileMsg.Sender)
                        {
                            IChatCallback sndrCallback = _clients[sender];
                            sndrCallback.ReceiveWhisper(msg, receiver);
                            return true;
                        }
                    }
                }
            }
            return false;
        }*/

        public void IsWriting(Client client)
        {
            foreach (IChatCallback callback in _clients.Values)
            {
                callback.IsWritingCallback(client);
            }
        }