2012-12-19 83 views
4

我有一个SignalR集线器,我从JQuery成功调用。从.net代码和JavaScript调用SignalR集线器

public class UpdateNotification : Hub 
{ 
    public void SendUpdate(DateTime timeStamp, string user, string entity, string message) 
    { 
     Clients.All.UpdateClients(timeStamp.ToString("yyyy-MM-dd HH:mm:ss"), user, entity, message);  
    } 
} 

更新消息从JS成功发送,像这样

var updateNotification = $.connection.updateNotification; 
$.connection.hub.start({ transport: ['webSockets', 'serverSentEvents', 'longPolling'] }).done(function() { }); 
updateNotification.server.sendUpdate(timeStamp, user, entity, message); 

,并成功接收,像这样

updateNotification.client.UpdateClients = function (timeStamp, user, entity, message) { 

我不能工作如何从我的控制器中调用sendUpdate。

回答

5

从你的控制器,在同一应用程序为枢纽(而不是从其他地方,作为一个.NET客户端),你让中心电话是这样的:

var hubContext = GlobalHost.ConnectionManager.GetHubContext<UpdateNotification>(); 
hubContext.Clients.All.yourclientfunction(yourargs); 

广播在从集线器外部集线器附近的脚下https://github.com/SignalR/SignalR/wiki/Hubs

要调用您的自定义方法有点不同。可能最好创建一个静态方法,然后您可以使用它来调用hubContext,因为OP在此处有:Server to client messages not going through with SignalR in ASP.NET MVC 4

3

以下是SignalR的示例quickstart 您需要创建一个集线器代理

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     // Connect to the service 
     var hubConnection = new HubConnection("http://localhost/mysite"); 

     // Create a proxy to the chat service 
     var chat = hubConnection.CreateHubProxy("chat"); 

     // Print the message when it comes in 
     chat.On("addMessage", message => Console.WriteLine(message)); 

     // Start the connection 
     hubConnection.Start().Wait(); 

     string line = null; 
     while((line = Console.ReadLine()) != null) 
     { 
      // Send a message to the server 
      chat.Invoke("Send", line).Wait(); 
     } 
    } 
} 
相关问题