4

JQueryから正常に呼び出す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 を呼び出す方法がわかりません。

4

2 に答える 2

6

コントローラーから、ハブと同じアプリケーションで (.NET クライアントとして他の場所からではなく)、次のようにハブ呼び出しを行います。

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

https://github.com/SignalR/SignalR/wiki/Hubsのふもと近くのハブの外側からのハブ経由のブロードキャストを参照してください。

カスタム メソッドを呼び出す方法は少し異なります。OP がここにあるように、hubContext を呼び出すために使用できる静的メソッドを作成するのがおそらく最善です:サーバーからクライアントへのメッセージは、ASP.NET MVC 4 で SignalR を使用して通過しません。

于 2012-12-19T13:01:24.873 に答える
3

SignalRクイック スタートの例を次に示します。ハブ プロキシ を作成する必要があります。

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();
        }
    }
}
于 2012-12-19T10:50:24.647 に答える