private async void SendButton_Clicked(object sender, EventArgs e) { if (_state != ViewState.Connected) { await DisplayAlert("Error", "Must be Connected to Send!", "OK"); return; } try { var UserName = !string.IsNullOrEmpty(AppStatics.UserProfile?.UserName) ? AppStatics.UserProfile?.UserName : txtUserName.Text; var receiveName = "*****@*****.**"; if (Device.RuntimePlatform == Device.Android) { receiveName = txtNameReceive.Text; } await Helper.HubCon.Connection.SendAsync("SendMessage", receiveName, MessageEntry.Text); //call api storge message in database _ = new Services.HttpExtension <BLL.M.Identity.ResponseMessage>().Post("DetailsChat/SendMessage", new BLL.M.Chat.SaveDetailsChatDto { sessionID = connectionId, message = MessageEntry.Text, }); AppendMessage(new BLL.M.Mobile.MessageModel { User = UserName, Message = MessageEntry.Text, IsOwnMessage = true, IsSystemMessage = false, }); MessageEntry.Text = ""; } catch (Exception ex) { DisplayAlert("Error", $"An error occurred while connecting: {ex}", "Ok"); } }
private async void ConnectDisconnectEvent(object sender, EventArgs e) { StackInsertEmail.IsVisible = false; UpdateState(ViewState.Connecting); try { var UserName = !string.IsNullOrEmpty(AppStatics.UserProfile?.UserName) ? AppStatics.UserProfile?.UserName : txtUserName.Text; Helper.HubCon.Connection.On <string>("ReceiveMessage", (ObjectMessageJson) => { //var Message = JsonConvert.DeserializeObject<BLL.M.Chat.DetailsMessage>(ObjectMessageJson); //AppendMessage(new BLL.M.Mobile.MessageModel //{ // Message = Message.Text, // IsOwnMessage = false, // IsSystemMessage = true, //}); //Helper.HubCon.Connection.InvokeAsync("EditInfoMessageIsReading", msg.Id); _ = new Services.HttpExtension <BLL.M.Identity.ResponseMessage>().Post("DetailsChat/IsReadMessage", new BLL.M.Chat.UpdateDetailsChatIsReadingDto { id = 0, isReading = true }); }); Helper.HubCon.Connection.On <string>("LoadOldMessage", (Object) => { var msg = JsonConvert.DeserializeObject <BLL.M.Mobile.MessageModel>(Object); InsertMessage(new BLL.M.Mobile.MessageModel { User = msg.User, Message = msg.Message, IsOwnMessage = msg.From == UserName, IsSystemMessage = msg.From != UserName, }); }); //Other Method to check if there no connect before //if not there, then create one if (string.IsNullOrEmpty(AppStatics.UserProfile?.Email)) { await Helper.HubCon.Connection.InvokeAsync("Connect"); } connectionId = Helper.HubCon.Connection.ConnectionId; _ = new Services.HttpExtension <BLL.M.Identity.ResponseMessage>().Post("HeaderChat/StartChat", new BLL.M.Chat.SaveHeaderChatDto { eMail = UserName, mobile = "0785461900", sessionId = connectionId, }); } catch (Exception ex) { _ = DisplayAlert("Error", $"An error occurred while connecting: {ex}", "Ok"); //AppendMessage(new BLL.M.Mobile.MessageModel { Message = $"An error occurred while connecting: {ex}" ,IsOwnMessage=true}); UpdateState(ViewState.Disconnected); return; } UpdateState(ViewState.Connected); }