简体   繁体   English

SignalR - 从服务器端代码调用Hub类方法(存在于单独的MVC项目中)

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

I have read some SignalR tutorials. 我已经阅读了一些SignalR教程。 In order to implement SignalR in my existing Asp.Net solution application I did following: 为了在我现有的Asp.Net解决方案应用程序中实现SignalR,我做了以下工作:

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. 我创建了一个单独的MVC 4项目,并创建了一个Hub类以及一些方法,以便可以从现有解决方案中存在的任何其他MVC项目/客户端调用它,这意味着我只想将其用作服务。

But now I have two questions: 但现在我有两个问题:

  1. How can I call it from some other MVC project. 我怎样才能从其他MVC项目中调用它。 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: 它是否与教程中提到的方式相同,例如在我的.cshtml页面的头部添加必要的脚本文件并使用如下的js脚本:

    var hub = $.connection.; var hub = $ .connection。; //and then //接着

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

  2. I want to call Hub code also directly from the Server side code. 我想直接从服务器端代码调用Hub代码。 Did it before when I was using SuperSocket. 在我使用SuperSocket之前做过它。 Would like to do same approach using SignalR. 想用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. 基本上,您使用ConnectionManager来呼叫集线器的客户端。

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. 要从应用程序外部调用它,我将使用某种公开的方法或服务并包装上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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