gpt4 book ai didi

c# - SignalR 2.0.0.0 : $. 连接未定义

转载 作者:太空宇宙 更新时间:2023-11-03 19:12:43 24 4
gpt4 key购买 nike

我正在尝试运行我的第一个 SignalR v2 项目,但运气不好,$.connection 未定义。

这是来自网络控制台的错误:

  1. 未捕获类型错误:无法读取未定义(匿名函数)的属性“chatHub”
  2. k
  3. l.fireWith
  4. p.extend.ready
  5. D

我的中心:

using Microsoft.AspNet.SignalR;

namespace InstantMessage
{
public class ChatHub : Hub
{
public void Hello()
{
Clients.All.hello();
}
}
}

启动.cs

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(InstantMessage.Startup))]
namespace InstantMessage
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}

字体:

<head>
.....
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<!--Reference the SignalR library. -->
<script src="~/Scripts/jquery.signalR-2.0.0.js"></script>
<!--Reference the autogenerated SignalR hub script. -->
<script src="~/signalr/hubs"></script>
</head>
<body>
<h2>Instant Message Demo</h2>

<div class="container">
<input type="text" id="message" />
<input type="button" id="sendmessage" value="Send" />
<input type="hidden" id="displayname" />
<ul id="discussion">
</ul>
</div>


<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
// Declare a proxy to reference the hub.
console.log($.connection);

var chat = $.connection.chatHub;
// Create a function that the hub can call to broadcast messages.
chat.client.broadcastMessage = function (name, message) {
// Html encode display name and message.
var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>' + encodedName
+ '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.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();
});
});
});
</script>

</body>

似乎/signalr/hubs 中的 js 代码是正确的,chathub 在那里并且文件的自动生成工作正常。

$.hubConnection.prototype.createHubProxies = function () {
var proxies = {};
this.starting(function () {
// Register the hub proxies as subscribed
// (instance, shouldSubscribe)
registerHubProxies(proxies, true);

this._registerSubscribedHubs();
}).disconnected(function () {
// Unsubscribe all hub proxies when we "disconnect". This is to ensure that we do not re-add functional call backs.
// (instance, shouldSubscribe)
registerHubProxies(proxies, false);
});

proxies.chatHub = this.createHubProxy('chatHub');
proxies.chatHub.client = { };
proxies.chatHub.server = {
hello: function () {
return proxies.chatHub.invoke.apply(proxies.chatHub, $.merge(["Hello"], $.makeArray(arguments)));
}
};

return proxies;
};

还应该提到我从 nuget 安装了 Signalr 并且我使用的是 VS2012。

最佳答案

Global.asax.cs 中删除 BundleConfig.RegisterBundles(BundleTable.Bundles); 为我解决了这个问题。我遇到这个问题是因为 jQuery 被包含了两次。

关于c# - SignalR 2.0.0.0 : $. 连接未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19444385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com