calling the Caller method in SignalR hub outside t

2019-05-02 11:55发布

问题:

I have a question in my mind about the Caller method of SignalR. In the hub method we can call a client side function like this.

Clients.Caller.addContosoChatMessageToPage(name, message);

but when i use to call it from outside the hub context it is not found or not implemented?? like this..

 var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
 context.Clients.Caller.reportProgress(recordCount,totalCount);

Can someone enlighten me in this part or is there other way to implement it.. by now i use to implement this

 var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
 context.Clients.User(CurrentUser.Usernm).reportProgress(recordCount,totalCount);

but now we are not claim based authentication so it will be a problem if the same usernm are logged..

回答1:

Outside of the hub, there obviously is no caller because the server is the one who initiates.

If you are worried about unique user names, you'll need to implement a custom IUserIdProvider, or you need to manage connection ids per user in some other way. Then you could call

context.Clients.Client(connectionId).reportProgress();

which would be unique.