.NET SignalR Error: Cannot read property 'client' of undefined
.NET SignalR Error: Cannot read property 'client' of undefined
我目前正在尝试在 .NET 网络应用程序中使用 SignalR。查阅了无数 posts/articles 之后,我仍然很困惑。在页面加载时,我收到错误消息:
Uncaught TypeError: Cannot read property 'client' of undefined
我目前的实现如下:
BundleConfig.cs:
//note jquery is rendered in a shared bundle in _layout.cshtml. I have
//verified in Chrome debugger it is loaded before the bundles shown here
bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
"~/Scripts/jquery.signalR-2.2.3.js"));
bundles.Add(new ScriptBundle("~/bundles/home")
.Include("~/Scripts/Home/Index.js"));
在视图中呈现的包:
@section scripts {
@Scripts.Render("~/bundles/signalr")
@Scripts.Render("~/bundles/home")
}
Startup.cs:
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
DashboardHub.cs:
namespace Sample.SignalR
{
[HubName("dashboardHub")]
public class DashboardHub : Hub
{
public void sendMessage(string message)
{
Clients.All.receiveMessage(notes);
}
}
}
Index.js:
$(function () {
var messageHub = $.connection.dashboardHub;
//send message to hub so hub updates other clients
$(document).on("submit", "#messageForm", function () {
messageHub.server.sendMessage($("#Message").val());
});
//receive message
messageHub.client.receiveMessage= function (message) {
$("#Message").empty();
$("#Message").text(notes);
}
$.connection.hub.start();
});
在线脚本中的页面加载时引发错误:
messageHub.client.receiveNotesUpdate = function (notes) {
有些事情我已经检查并仔细检查过:
- 脚本以正确的顺序呈现在页面上
- 集线器名称和方法名称在客户端和服务器之间匹配(已尝试使用
并且没有
[HubName("dashboardHub")]
注释)
- 集线器 class 是 public
我不确定为什么 messageHub
此时未定义。我该如何解决这个问题?
我认为您错过了添加自动生成的 SignalR Hub 脚本
<script src="signalr/hubs"></script>
就我而言,我的信号中心在网络上api。我包含了我的 api 所在的域:
<script src="somedomainorinstance/signalr/hubs"></script>
同样在您的页面加载中,您可以添加此内容以查看是否正在建立连接
$.connection.hub.url = url + "/signalr/hubs";
$.connection.hub.logging = true;
$.connection.hub.start()
.done(function () { console.log("Signal R connected"); })
.fail(function () { console.log("Could not Connect to signal R hub!"); });
//I added this to established the connection again in case of a disconnect
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000); // Re-start connection after 5 seconds
});
//put the rest of your code here
这就是我现在的设置方式。它工作得很好。
希望对您有所帮助!干杯!
我目前正在尝试在 .NET 网络应用程序中使用 SignalR。查阅了无数 posts/articles 之后,我仍然很困惑。在页面加载时,我收到错误消息:
Uncaught TypeError: Cannot read property 'client' of undefined
我目前的实现如下:
BundleConfig.cs:
//note jquery is rendered in a shared bundle in _layout.cshtml. I have
//verified in Chrome debugger it is loaded before the bundles shown here
bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
"~/Scripts/jquery.signalR-2.2.3.js"));
bundles.Add(new ScriptBundle("~/bundles/home")
.Include("~/Scripts/Home/Index.js"));
在视图中呈现的包:
@section scripts {
@Scripts.Render("~/bundles/signalr")
@Scripts.Render("~/bundles/home")
}
Startup.cs:
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
DashboardHub.cs:
namespace Sample.SignalR
{
[HubName("dashboardHub")]
public class DashboardHub : Hub
{
public void sendMessage(string message)
{
Clients.All.receiveMessage(notes);
}
}
}
Index.js:
$(function () {
var messageHub = $.connection.dashboardHub;
//send message to hub so hub updates other clients
$(document).on("submit", "#messageForm", function () {
messageHub.server.sendMessage($("#Message").val());
});
//receive message
messageHub.client.receiveMessage= function (message) {
$("#Message").empty();
$("#Message").text(notes);
}
$.connection.hub.start();
});
在线脚本中的页面加载时引发错误:
messageHub.client.receiveNotesUpdate = function (notes) {
有些事情我已经检查并仔细检查过:
- 脚本以正确的顺序呈现在页面上
- 集线器名称和方法名称在客户端和服务器之间匹配(已尝试使用
并且没有
[HubName("dashboardHub")]
注释) - 集线器 class 是 public
我不确定为什么 messageHub
此时未定义。我该如何解决这个问题?
我认为您错过了添加自动生成的 SignalR Hub 脚本
<script src="signalr/hubs"></script>
就我而言,我的信号中心在网络上api。我包含了我的 api 所在的域:
<script src="somedomainorinstance/signalr/hubs"></script>
同样在您的页面加载中,您可以添加此内容以查看是否正在建立连接
$.connection.hub.url = url + "/signalr/hubs";
$.connection.hub.logging = true;
$.connection.hub.start()
.done(function () { console.log("Signal R connected"); })
.fail(function () { console.log("Could not Connect to signal R hub!"); });
//I added this to established the connection again in case of a disconnect
$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000); // Re-start connection after 5 seconds
});
//put the rest of your code here
这就是我现在的设置方式。它工作得很好。
希望对您有所帮助!干杯!