public void Say(Message msg) { foreach (IChatCallback callback in _clients.Values) { callback.Receive(msg); } }
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; } } } } }
public IAsyncResult BeginReceive(Message msg, AsyncCallback callback, object asyncState) { throw new NotImplementedException(); }
public IAsyncResult BeginReceiveWhisper(Message msg, SharedComponents.Client receiver, AsyncCallback callback, object asyncState) { throw new NotImplementedException(); }
/// <summary> /// Receives a whisper and decrypts it /// </summary> /// <param name="msg"></param> /// <param name="receiver"></param> public void ReceiveWhisper(Message msg, SharedComponents.Client receiver) { foreach (SharedComponents.Client c in _onlineClients.Values) { if (c.Name == msg.Sender) { string output = ""; switch (_viewModel.SelectedCypher) { case (int)Cyphers.SimpleEncryption: var cypher1 = new SimpleEncryption(); output = cypher1.DecryptMessage(msg.Content, _viewModel.Key); break; case (int)Cyphers.Rijndael: var cypher2 = new Rijndael(); output = cypher2.DecryptMessage(msg.Content, _viewModel.Key); break; } ListBoxItem item = MakeItem(c.AvatarID, msg.Sender + " whispers " + receiver.Name + " : " + output); _viewModel.ChatMsgs.Add(item); _messages.Add(msg.Content); _msgIds.Add(msg.Sender + " whispers " + receiver.Name); } } _viewModel.ScrollDown(); }
/// <summary> /// Handles message receiving and decryption of incoming messages. /// </summary> /// <param name="msg"></param> public void Receive(Message msg) { foreach (SharedComponents.Client c in _onlineClients.Values) { if (c.Name == msg.Sender) { string output = ""; switch (_viewModel.SelectedCypher) { case (int)Cyphers.SimpleEncryption: var cypher1 = new SimpleEncryption(); output = msg.Sender + " : " + cypher1.DecryptMessage(msg.Content, _viewModel.Key); break; case (int)Cyphers.Rijndael: var cypher2 = new Rijndael(); output = msg.Sender + " : " + cypher2.DecryptMessage(msg.Content, _viewModel.Key); break; } ListBoxItem item = MakeItem(c.AvatarID, output); _viewModel.ChatMsgs.Add(item); _messages.Add(msg.Content); using (var terr = new ChatDataContainer()) { var sender = terr.ChatUsersSet.Where(s => s.Username == msg.Sender).ToList(); var curUser = terr.ChatUsersSet.Where(s => s.Username == _viewModel.Name).ToList(); ChatData temp = new ChatData { Id = curUser[0].Id, Sender = sender[0].Id, Message = msg.Content, RecTime = msg.Time, ChatUsers = curUser[0] }; _msgIds.Add(msg.Sender); try { terr.ChatDataSet.Add(temp); terr.SaveChanges(); } catch (Exception e) { _viewModel.Name = e.ToString(); } } } } _viewModel.ScrollDown(); }
/// <summary> /// This method encrypts the message we want to send /// and sends it either to one recipient (whisper) /// or all online chat clients /// </summary> public void Send() { if (_channel != null && _viewModel.ChatInputText != "") { if (_channel.State == CommunicationState.Faulted) { HandleProxy(); } else { //Create message, assign its properties //encrypt message string temp = ""; switch (_viewModel.SelectedCypher) { case (int)Cyphers.SimpleEncryption: var cypher1 = new SimpleEncryption(); temp = cypher1.EncryptMessage(_viewModel.ChatInputText, _viewModel.Key); break; case (int)Cyphers.Rijndael: var cypher2 = new Rijndael(); temp = cypher2.EncryptMessage(_viewModel.ChatInputText, _viewModel.Key); break; } Message msg = new Message { Sender = _localClient.Name, Time = DateTime.Now, Content = temp }; //If whisper mode is checked and an item is //selected in the list box of clients, it will //arrange a client object called receiver //to whisper if (_viewModel.WhisperBox) { if (_receiver != null) { _channel.WhisperAsync(msg, _receiver); _viewModel.ChatInputText = ""; } } else { _channel.SayAsync(msg); _viewModel.ChatInputText = ""; } //Tell the service to tell back all clients that this client //has just finished typing.. _channel.IsWritingAsync(null); } } }