简体   繁体   中英

how Call a signalR function just for web API Caller by c#

I need to callback a function on the client who has just called the Web API method. I can send back to all users but I just need to call for JUST the caller.

        [HttpPost]
        public WebResult Logout()
        {
     var cc =Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<EventsHub>();
         cc.Clients.All.TestLogOut();
        }

I know it might be possible if I have the connectionId for the caller, but I do not know how it can be?

Plus, it seems there is something IHbubCallerContext... what is it and can it be helpful. if can get the caller and the connectioId!

cc.Clients.Client("ConnectionId").TestLogoutCurrent();

HOW TO CallBack a Caller function OR how to get the caller connectionId ?

thanks

You can call a function on the Caller client from within your SignalR Hub implementation:

[HubName("MyHub")]
public class MyHub : Hub
{
    public void logout()
    {
        Clients.Caller.clientLogout();
    }
}

Finally I found a solution , it seems we cannot get connection Id in web API, so we need to create a HUB class and call a method from client. in that method we have caller and connection ID, so we are able to callback on this method.

public class EventHub : Hub

    {
        public void LogOutSignalRTest()
        {

            var cc = Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<EventsHub>();
                cc.Clients.Client(Context.ConnectionId).clientlogout();

        }

    }

the context here is type of HubCallerContext, so we have the connection ID , clientlogout is a function on client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM