[英]Signalr Sending messages from server to client c# An unhandled exception has occurred while executing the request
[英]Sending Messages from the Server to the Client with SignalR
我一直在閱讀文檔中的這篇文章:
它說,我可以像這樣從客戶端向服務器發送消息:
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
但是,如何從服務器向客戶端發送消息?
我首先遵循了本教程http://www.codemag.com/Article/1210071 ,該教程說明我只需要這樣做:
SendMessage("Index action invoked.");
使用SendMessage()
定義為:
private void SendMessage(string message)
{
GlobalHost.ConnectionManager.GetHubContext<NotificationHub>().Clients.All.sendMessage(message);
}
但是,這不起作用。 在我的客戶端上,我的錯誤是:
對象#沒有方法“激活”
我正在使用的客戶端代碼是:
$.connection.hub.start(function () {
notificationHub.activate(function (response) {
/*
$("#target")
.find('ul')
.append($("<li></li>").html(response));
*/
console.log(response);
});
});
因此,問題是,如何從服務器向客戶端發送簡單消息?
有人可以向我展示如何執行此操作的完整示例嗎? 我已經從文檔中看到了股票行情自動收錄器示例,但很難理解/應用。
據我了解,您正在此處調用名為sendMessage的客戶端上的函數
private void SendMessage(string message)
{
GlobalHost.ConnectionManager.GetHubContext<NotificationHub>().Clients.All.sendMessage(message);
}
但是,我沒有在客戶端看到此功能的定義。 相反,您定義了一個稱為activate()的函數。 另外,從我的工作代碼中,我已經定義了這樣的客戶端功能。
var hub = $.connection.notificationHub;
hub.client.sendMessage= function (data) {
//some logic here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.