简体   繁体   中英

SignalR - Calling Hub class method (present in a separate MVC project) from Server side code

I have read some SignalR tutorials. In order to implement SignalR in my existing Asp.Net solution application I did following:

I have created a separate MVC 4 project and created a Hub class along with some methods, so that it can be called from any other MVC project/Client present in the existing solution, means I would just like to use it as a service.

But now I have two questions:

  1. How can I call it from some other MVC project. Will it be the same way as mentioned in the tutorials, example like adding necessary script files in head of my .cshtml page and using js script like following:

    var hub = $.connection.; //and then

    hub.server.send("some args");

  2. I want to call Hub code also directly from the Server side code. Did it before when I was using SuperSocket. Would like to do same approach using SignalR. How can I do it ?

If you answer, then kindly please give some code sample(s). Many Thanks.

There is sample code for calling client methods from outside the Hub class (but within the application) here . Basically, you use the ConnectionManager to call a hub's clients.

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.myClientMethod(myValue);

To call this from outside your application, I'd use some sort of exposed method or service and wrap the above code.

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