Get SignalrR ConnectionId on ClientSide (Angular A

2019-06-16 21:11发布

问题:

Hi I'm using an angular app to connect to signalr, I am using "@aspnet/signalr": "1.0.0-preview1-final".

I need to send the connectionId to my controller (I need to do some process and tell all others clients but no the user making the request), the problem is Connection Id is private, there is a way to get the connection Id?

回答1:

Seems like an XY problem.

Tell all other clients (problem X)

Hub:

public async Task TellAllOtherClients(object[] args)
{
    await Clients.Others.SendAsync("method", args);
}

Get connectionId (solution Y)

Hub:

public string GetConnectionId()
{
    return Context.ConnectionId;
}

Client:

hub.invoke('getConnectionId')
    .then(function (connectionId) {
        // Send the connectionId to controller
    });