Skip to content

Commit

Permalink
Added new connection methods to keep track of user and send messages …
Browse files Browse the repository at this point in the history
…to users in a specific room.
  • Loading branch information
Ariarley Dospassos authored and Ariarley Dospassos committed Feb 20, 2021
1 parent dc3833f commit 8914a8e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 33 deletions.
43 changes: 26 additions & 17 deletions Gordon360/ApiControllers/DirectMessageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,7 @@ public DirectMessageController(IDirectMessageService DirectMessageService)
{
_DirectMessageService = DirectMessageService;
}

/// <summary>
/// returns hello world example
/// </summary>
/// <returns>string and date</returns>
[HttpGet]
[Route("")]
public IHttpActionResult Get()
{
DateTime currentTime = DateTime.Now;
var result = "hello world I'm coming from the back end at: " + currentTime;

return Ok(result);
}


/// <summary>
/// returns messages from a specified group
/// </summary>
Expand Down Expand Up @@ -199,6 +185,8 @@ public IHttpActionResult StoreUserRooms([FromBody] String roomId)

}



/// <summary>
/// stores connection associated with a user id
/// </summary>
Expand All @@ -207,7 +195,6 @@ public IHttpActionResult StoreUserRooms([FromBody] String roomId)
[Route("userConnectionIds")]
public IHttpActionResult StoreUserConnectionIds([FromBody] String connectionId)
{

var authenticatedUser = this.ActionContext.RequestContext.Principal as ClaimsPrincipal;
var username = authenticatedUser.Claims.FirstOrDefault(x => x.Type == "user_name").Value;

Expand Down Expand Up @@ -247,7 +234,29 @@ public IHttpActionResult GetConnectionIds([FromBody] String userId)

}

/// <summary>
/// Deletes connection ids associated with a user id
/// </summary>
/// <returns>true if successful</returns>
[HttpPut]
[Route("deleteUserConnectionIds")]
public IHttpActionResult DeleteConnectionIds([FromBody] String connectionId)
{

}

var result = _DirectMessageService.DeleteUserConnectionIds(connectionId);

if (result == null)
{
return NotFound();
}


return Ok(result);

}


}

}
12 changes: 6 additions & 6 deletions Gordon360/Documentation/Gordon360.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 37 additions & 10 deletions Gordon360/Hubs/SignalRHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,58 @@
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Gordon360;
using Gordon360.Services;
using Gordon360.ApiControllers;
using System.Web.Http;
using Gordon360.Models.ViewModels;
using Gordon360.Repositories;
using System.Security.Claims;

namespace Gordon360.Hubs
{
[RoutePrefix("api/signalr")]
[HubName("ChatHub")]
public class ChatHub : Hub
{

public async Task refreshMessages(string user)
{
await Clients.All.SendAsync("ReceivedMessagerefresh", user);
//public DirectMessageService _DirectMessageService = new DirectMessageService();

public async Task refreshMessages(List<string> connectionIds, IEnumerable<MessageViewModel> message)
{
foreach(string connections in connectionIds)
{
await Groups.Add(connections, "list");
}
await Clients.Group("list").SendAsync("Received message refresh, Message: ", message);
}

public async Task test()

public string savedUserId;
public string savedConnectionId;

public void saveConnection(string id)
{
await Clients.All.SendAsync("It's working boss");
DirectMessageService _DirectMessageService = new DirectMessageService();
savedUserId = id;
savedConnectionId = Context.ConnectionId;
_DirectMessageService.StoreUserConnectionIds(id, Context.ConnectionId);
Clients.All.BroadcastMessage("It's working boss");

}

public string test2()

public override Task OnDisconnected(bool stopCalled)
{
DirectMessageService _DirectMessageService = new DirectMessageService();

_DirectMessageService.DeleteUserConnectionIds(Context.ConnectionId);
return base.OnDisconnected(stopCalled);
}

public override Task OnReconnected()
{
return "It worked";
DirectMessageService _DirectMessageService = new DirectMessageService();

_DirectMessageService.DeleteUserConnectionIds(savedConnectionId);
_DirectMessageService.StoreUserConnectionIds(savedUserId, Context.ConnectionId);
return base.OnReconnected();
}
}

Expand Down
23 changes: 23 additions & 0 deletions Gordon360/Services/DirectMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public bool SendMessage(SendTextViewModel textInfo, String user_id)

}



public bool StoreUserRooms(String userId, String roomId)
{
var _unitOfWork = new UnitOfWork();
Expand Down Expand Up @@ -258,6 +260,7 @@ public bool StoreUserRooms(String userId, String roomId)

}


public bool StoreUserConnectionIds(String userId, String connectionId)
{
var _unitOfWork = new UnitOfWork();
Expand Down Expand Up @@ -314,6 +317,26 @@ public IEnumerable<ConnectionIdViewModel> GetUserConnectionIds(String userId)

}

public bool DeleteUserConnectionIds(String connectionId)
{
var connectionIdParam = new SqlParameter("@connection_id", connectionId);

var result = RawSqlQuery<MessageViewModel>.query("DELETE_USER_CONNECTION_ID @connection_id", connectionIdParam); //run stored procedure

bool returnAnswer = true;

if (result == null)
{
returnAnswer = false;
throw new ResourceNotFoundException() { ExceptionMessage = "The data was not found." };
}



return returnAnswer;

}


}

Expand Down
1 change: 1 addition & 0 deletions Gordon360/Services/ServiceInterfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public interface IDirectMessageService
bool SendMessage(SendTextViewModel textInfo, string user_id);
bool StoreUserRooms(String userId, String roomId);
bool StoreUserConnectionIds(String userId, String connectionId);
bool DeleteUserConnectionIds(String connectionId);
IEnumerable<ConnectionIdViewModel> GetUserConnectionIds(String userId);
IEnumerable<MessageViewModel> GetMessages(string roomId);
IEnumerable<GroupViewModel> GetRooms(string userId);
Expand Down

0 comments on commit 8914a8e

Please sign in to comment.