public void ChatRequestReceived(ChatRequest req) { if (!_chatRequestActive) { ctx.PeerUserName = req.From; _navigationService.NavigateTo(ViewModelLocator.ChatPageKey); ChatRequestResponse resp = new ChatRequestResponse { Success = true, ErrorMessage = "", From = CurrentUserName, To = req.From }; ctx.Dispatcher.SendObjectAsync(resp); Window.Current.Activate(); } }
private void HandleChatResponseMsg(ChatRequestResponse resp, TalkBackDBContext dbContext) { // first check that the addressee is in the dictionary: WebSocket peer; if (!WebSocketMiddleware._sockets.TryGetValue(resp.To, out peer)) { // not found -- ignore return; } string strMsg = resp.ToXml().ToString(); WebSocketMiddleware.SendStringAsync(peer, strMsg); return; }
public void ChatResponseReceived(ChatRequestResponse resp) { if (resp.Success && _chatRequestActive) { timer.Cancel(); _chatRequestActive = false; _navigationService.NavigateTo(ViewModelLocator.ChatPageKey); Window.Current.Activate(); return; } if (!resp.Success && _chatRequestActive) { timer.Cancel(); _chatRequestActive = false; _dialogService.ShowError(resp.ErrorMessage, "Error", "OK", null); } }
private void HandleChatMessageMsg(MessageModel msg, TalkBackDBContext dbContext) { // first check that the addressee is in the dictionary: WebSocket peer; if (!WebSocketMiddleware._sockets.TryGetValue(msg.To, out peer)) { // not found -> send a ChatRequestResponse with failure: ChatRequestResponse resp = new ChatRequestResponse { Success = false, ErrorMessage = "Chat peer is not online" }; WebSocketMiddleware.SendStringAsync(_webSocket, resp.ToXml().ToString()); return; } // save message in the log: Message dbMessage = new Message { Content = msg.Content, ReceiverName = msg.To, SenderName = msg.From, Time = DateTime.Now }; try { dbContext.Message.Add(dbMessage); dbContext.SaveChanges(); } catch (Exception ex) { //do nothing } // send the message to both sender and receiver: string strMsg = msg.ToXml().ToString(); WebSocketMiddleware.SendStringAsync(_webSocket, strMsg); WebSocketMiddleware.SendStringAsync(peer, strMsg); return; }
private void HandleChatRequestMsg(ChatRequest req, TalkBackDBContext dbContext) { // first check that the addressee is in the dictionary: WebSocket peer; if (!WebSocketMiddleware._sockets.TryGetValue(req.To, out peer)) { // not found -> send a ChatRequestResponse with failure: ChatRequestResponse resp = new ChatRequestResponse { Success = false, ErrorMessage = "Chat peer is not online" }; WebSocketMiddleware.SendStringAsync(_webSocket, resp.ToXml().ToString()); return; } string strMsg = req.ToXml().ToString(); WebSocketMiddleware.SendStringAsync(peer, strMsg); return; }
private async void DispatchSingleObjectMsg(XElement xmlObj) { ModelXmlMapper.MappedType typeInElement = GetMappedType(xmlObj); switch (typeInElement) { case ModelXmlMapper.MappedType.MESSAGE: MessageModel msg = new MessageModel(); if (msg.FromXml(xmlObj)) { string message = msg.From + ": " + msg.Content; await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { context.ChatWinodw.MessageReceived(message); } ); } break; case ModelXmlMapper.MappedType.CHAT_REQUEST: ChatRequest req = new ChatRequest(); if (req.FromXml(xmlObj)) { // TODO: check if ongoing chat and return failure from here: await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { context.ContactsWindow.ChatRequestReceived(req); } ); } break; case ModelXmlMapper.MappedType.CHAT_REQUEST_RESPONSE: ChatRequestResponse resp = new ChatRequestResponse(); if (resp.FromXml(xmlObj)) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { context.ContactsWindow.ChatResponseReceived(resp); // context.ChatWinodw.Disconnected(resp); } ); } break; case ModelXmlMapper.MappedType.LOGIN_RESPONSE: LoginResponse loginResp = new LoginResponse(); if (loginResp.FromXml(xmlObj)) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { context.LoginWindow.OnLoginResponseAsync(loginResp); } ); } break; case ModelXmlMapper.MappedType.CONTACT: Contact contact = new Contact(); if (contact.FromXml(xmlObj)) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( CoreDispatcherPriority.Normal, () => { if (context.ContactsWindow != null) { context.ContactsWindow.OnContactUpdate(contact); } } ); } break; default: // do nothing break; } }
internal void Disconnected(ChatRequestResponse resp) { }
public void HandleMessage(string incommingMessage, TalkBackDBContext dbContext) { MappedType t = MappedType.UNDEFINED; XDocument serialized = XDocument.Parse(incommingMessage); XElement typeElement = serialized.Root.Element("Type"); if (typeElement == null) { // ignore return; } string type = typeElement.Value; if (ModelXmlMapper.map.TryGetValue(type, out t)) { switch (t) { case MappedType.MESSAGE: MessageModel msg = new MessageModel(); if (msg.FromXml(serialized.Root)) { HandleChatMessageMsg(msg, dbContext); } break; case MappedType.USER: UserModel user = new UserModel(); user.FromXml(serialized.Root); break; case MappedType.CONTACT: Contact contact = new Contact(); contact.FromXml(serialized.Root); break; case MappedType.CHAT_REQUEST: ChatRequest req = new ChatRequest(); if (req.FromXml(serialized.Root)) { HandleChatRequestMsg(req, dbContext); } break; case MappedType.CHAT_REQUEST_RESPONSE: ChatRequestResponse resp = new ChatRequestResponse(); if (resp.FromXml(serialized.Root)) { HandleChatResponseMsg(resp, dbContext); } break; case MappedType.LOGIN: Login login = new Login(); if (login.FromXml(serialized.Root)) { HandleLoginMsg(login, dbContext); } break; case MappedType.LOGIN_RESPONSE: LoginResponse loginResp = new LoginResponse(); loginResp.FromXml(serialized.Root); break; case MappedType.UNDEFINED: throw new Exception("Don't know how to parse this type"); } } }