こちらのwiki記事に基づく:https ://github.com/SignalR/SignalR/wiki/Hubs
これにより、MVCアプリケーションでハブを介してメッセージをブロードキャストできるようになります。
$(function () {
// Proxy created on the fly
var chat = $.connection.chatterBox;
// Declare a function on the chat hub so the server can invoke it
chat.client.addMessage = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
// Start the connection
$.connection.hub.start().done(function () {
$("#broadcast").click(function () {
// Call the chat method on the server
chat.server.send($('#msg').val());
});
});
});
ServerHub.dllという名前の別のDLLにある私のハブは次のようになります
namespace ServerHub
{
public class ChatterBox : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);
}
}
}
したがって、上記の設定で、複数の異なるブラウザで同じURLを参照でき、1つのブラウザからメッセージを送信すると、他のすべてのブラウザに反映されます。
しかし、私が今やろうとしているのは、コントローラー内からメッセージを送信することです。
そのため、すぐに使用できるMVCインターネットアプリケーションのHomeControllerのアクションについて、次のように追加しました。
using ServerHub;
public ActionResult About()
{
ViewBag.Message = "Your app description page.";
var context = GlobalHost.ConnectionManager.GetHubContext<ChatterBox>();
context.Clients.All.say("HELLO FROM ABOUT");
return View();
}
しかし、上記は機能していないようです。エラーメッセージや実行時エラーはありません。コードが実行されますが、他のブラウザでメッセージが表示されません。
どこで私は間違えましたか?