private void SendMessage(ChatMember _from, object _msg, string _roomName, string _to = null, EventType _type = EventType.Message) { if (_from == null) { throw new ArgumentNullException("_from"); } if (String.IsNullOrWhiteSpace(_roomName)) { throw new ArgumentNullException("_roomName"); } if (_msg == null) { throw new ArgumentNullException("_msg"); } var room = GetChatRoom(_roomName); if (room == null) { throw new ArgumentException("_roomName"); } if (_to != null && room.GetMember(_to) == null && room.GetWaitingMember(_to) == null) { throw new ArgumentException("_to"); } var newEvent = EventFactory.NewEvent(_type); var jss = new JavaScriptSerializer(); newEvent.RoomName = _from.Name; newEvent.SenderName = _from.Name; newEvent.SenderEmail = _from.Email; newEvent.Text = jss.Serialize(_msg); newEvent.Timestamp = DateTime.Now; newEvent.RoomName = room.Name; var roomClients = room.Members.Where(c => c.Email != _from.Email); if (_to != null) { // direct message newEvent.Recipients.Add(_to); } else { // broadcast message newEvent.Recipients.AddRange(roomClients.Select(c => c.Email)); } messages.Add(newEvent); using (var db = new CloudTalkEntities()) { db.AddToHistory(new History { Room = _roomName, Client = db.Client.First(c => c.Id == room.OwnerId), Sender = _from.Email, Text = _msg.ToString(), Timestamp = DateTime.Now.ToString() }); db.SaveChanges(); } foreach (var item in roomClients) { SignalMember(item.Email); } }
public void AddMember(string _roomName, string _clientEmail, string _clientName) { if (String.IsNullOrWhiteSpace(_clientEmail)) { throw new ArgumentNullException("_clientEmail"); } if (String.IsNullOrWhiteSpace(_clientName)) { throw new ArgumentNullException("_clientName"); } if (String.IsNullOrWhiteSpace(_roomName)) { throw new ArgumentNullException("_roomName"); } var room = GetChatRoom(_roomName); if (room == null) { throw new ArgumentException("_roomName"); } if(!room.IsFull) { room.AddMember(_clientEmail, _clientName); SendMessage(_clientEmail, new { Message = "Entrou na sala" }, _roomName); } else { var waiter = new ChatMember { Name = _clientName, Email = _clientEmail }; room.WaitingMembers.Enqueue(waiter); // Let the client know his position on the queue throw new FullChatRoomException(String.Empty, RoomQueueSize(room.Name)); } }