Get SignalrR ConnectionId on ClientSide (Angular App)

感情迁移 提交于 2020-01-03 07:22:32

问题


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
    });



回答2:


Client:

let connection: signalR.HubConnection;
let accessToken = "3076a225-f2f6-4c68-b894-08accb62bb90";

connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:8178/notifyHub", { accessTokenFactory: () => accessToken }).build();
connection.start().then(function(){connection.invoke("GetConnectionId").then(function(connectionId){console.log(connectionId)})}).catch(err => console.error(err));


来源:https://stackoverflow.com/questions/50177099/get-signalrr-connectionid-on-clientside-angular-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!