[英]SignalR Hub not connecting
I'm trying to create a SignalR application but I cannot connect to my hub. 我正在尝试创建一个SignalR应用程序,但我无法连接到我的集线器。
Here is my following code: 这是我的以下代码:
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
</head>
<body>
<button id="display">Display</button>
<button id="send">Send</button>
<input id="Name" type="Text">
<input id="Content" type="Text">
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.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>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
$(function () {
messageArr = [];
var chat = $.connection.chatHub;
chat.client.broadcastMessage = function (id, name, content) {
var message = { ID: id, Name: name, Content: content };
messageArr.push(message);
};
$('#display').click(function() {
window.alert(messageArr);
});
$.connection.hub.start().done(function () {
$('#send').click(function () {
window.alert("Sent");
chat.server.send(0, document.getElementById("Name").value, document.getElementById("Content"));
});
});
});
</script>
</body>
</html>
C# Hub: C#Hub:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace WebAPI
{
public class ChatHub : Hub
{
public void Send(int id, String name, String content)
{
Clients.All.broadcastMessage(id, name, content);
}
}
}
Owin Startup Class: Owin启动课程:
using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(WebAPI.Startup))]
namespace WebAPI
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
app.MapSignalR();
}
}
}
When I run this through Visual Studio 2013, I click the send button and no alert comes up telling me I've pressed the button. 当我通过Visual Studio 2013运行时,我单击发送按钮,没有警报出现,告诉我我按下了按钮。 Also, when I click the display button later, the messageArr array is still empty.
此外,当我稍后单击显示按钮时,messageArr数组仍为空。 What can I change to make this actually work?
我可以改变什么来使其真正起作用?
Well, I'm not sure how but I seemed to have fixed my code 好吧,我不知道怎么样,但我好像修好了我的代码
<!DOCTYPE html>
<html>
<head>
<title>SignalR Simple Chat</title>
</head>
<body>
<button id="display"onclick="displayMessage()">Display</button>
<button id="send">Send</button>
<input id="Name" type="Text">
<input id="Content" type="Text">
<!--Script references. -->
<!--Reference the jQuery library. -->
<script src="Scripts/jquery-1.6.4.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>
<!--Add script to update the page and send messages.-->
<script type="text/javascript">
messageArr = [];
$(function () {
var chat = $.connection.chatHub;
chat.client.broadcastMessage = function (id, name, content) {
var message = { ID: id, Name: name, Content: content };
messageArr.push(message);
};
$.connection.hub.start().done(function () {
$('#send').click(function () {
chat.server.send(0, document.getElementById("Name").value, document.getElementById("Content").value);
});
});
});
function displayMessage() {
window.alert(messageArr);
}
</script>
If anyone could explain to me how this fixed my problem that would be helpful. 如果有人能向我解释这是如何解决我的问题会有所帮助的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.