I have a Swift iOS app that is connecting to my MVC .net core back-end app. The back-end has SignalR set up.
My Hub class on server:
public class ActivityHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
public override async Task OnConnectedAsync()
{
await base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception exception)
{
return base.OnDisconnectedAsync(exception);
}
}
My Startup.cs on server
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ActivityHub>("/activityHub");
});
My Connection initiation on client
static var _socketConnection: URLSessionWebSocketTask?
static func connectToSocket(){
let url = URL(string: "wss://mysite.net/activityHub")!
_socketConnection = defaultSession.webSocketTask(with: url)
_socketConnection?.resume()
}
When I use Vanilla web sockets on the server side without using SignalR all works fine except that there is no automatic management of connections etc, hence why I need to move to using SignalR to get the list of connections, users, etc, out of the box.
I however do not know how to specify the user property on the client, during the process of sending that socket connection request. I assume the user property that I need to specify is the some id of some sort that is guaranteed to be unique for the connect?
How do I do this?
The intent is to eventually have the server be able to send a message to a connection for a specific user, with information about specific events.
UPDATE
The connection made by SignalR has a ConnectionID property and that needs to be associated and mapped to a user, which is what I'm after. The resource is from 2014 so it may be a bit stale.
Also, most importantly, I believe, Microsoft says By default, SignalR uses the ClaimTypes.NameIdentifier from the ClaimsPrincipal associated with the connection as the user identifier
. So I might actually not have to do any mapping as I am using the Identity framework.
Be sure to use either .NET SignalR or ASP.NET CORE SignalR for both Server and Client.
You updated to using ASP.NET CORE SignalR (server) and a SignalRClient-Swift package that works with Core.
Good to hear it is working now.