public async Task JoinGroup(string groupName) { //calling Join group API ChatUserGroupModel joinGroup = new ChatUserGroupModel(); joinGroup.Group = groupName; joinGroup.User = Context.GetHttpContext().Request.Query["UserName"]; var joinGroupPayload = JsonConvert.SerializeObject(joinGroup); // Wrap our JSON inside a StringContent which then can be used by the HttpClient class var joinGrouphttpContent = new StringContent(joinGroupPayload, Encoding.UTF8, "application/json"); var joineGroupUri = new Uri(string.Format(joinGroupUrl, string.Empty)); HttpClient joinGroupClient = new HttpClient(); var joinGroupResponse = await joinGroupClient.PostAsync(joineGroupUri, joinGrouphttpContent); if (joinGroupResponse.IsSuccessStatusCode) { var contentResult = await joinGroupResponse.Content.ReadAsStringAsync(); var responseMessage = JsonConvert.DeserializeObject <APIMessageResponse>(contentResult); if (responseMessage.Message.StartsWith("error")) { await Clients.Caller.SendAsync("DisplayError", responseMessage.Message); } else { await Groups.AddToGroupAsync(Context.ConnectionId, groupName); //await Clients.Group(groupName).SendAsync("Send", $"{Context.ConnectionId} has joined the group {groupName}."); await Clients.Group(groupName).SendAsync("SendToGroup", $"{Context.GetHttpContext().Request.Query["UserName"]} is now online {groupName}.", groupName, groupName); await Clients.Caller.SendAsync("JoinGroupTab", groupName); //calling get group messages API getGroupMessagesUrl = getGroupMessagesUrl + groupName; var groupMessageUri = new Uri(string.Format(getGroupMessagesUrl, string.Empty)); HttpClient getGroupMessagesClient = new HttpClient(); var getGroupMessagesResponse = await getGroupMessagesClient.GetAsync(groupMessageUri); var messagesContent = await getGroupMessagesResponse.Content.ReadAsStringAsync(); IEnumerable <ChatMessageModel> groupMessagesVM = await Task.Run(() => JsonConvert.DeserializeObject <IEnumerable <ChatMessageModel> >(messagesContent.ToString())); foreach (ChatMessageModel msgVM in groupMessagesVM) { await Clients.Caller.SendAsync("PopulateMyGroupMessage", msgVM.GroupName, msgVM.SenderName, msgVM.Content); } } } }
public async Task RemoveFromGroup(string groupName) { await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName); await Clients.Group(groupName).SendAsync("SendToGroup", $"{Context.ConnectionId} has left the group {groupName}.", groupName, groupName); //calling remove from group API ChatUserGroupModel userGroup = new ChatUserGroupModel(); userGroup.Group = groupName; userGroup.User = Context.GetHttpContext().Request.Query["UserName"]; var userGroupPayload = JsonConvert.SerializeObject(userGroup); // Wrap our JSON inside a StringContent which then can be used by the HttpClient class var userGrouphttpContent = new StringContent(userGroupPayload, Encoding.UTF8, "application/json"); var leaveGroupUri = new Uri(string.Format(leavGroupUrl, string.Empty)); HttpClient leaveGroupClient = new HttpClient(); var leaveGroupResponse = await leaveGroupClient.PostAsync(leaveGroupUri, userGrouphttpContent); }