SignalR Hub未连接
问题描述:
我正在尝试创建SignalR应用程序,但无法连接到我的集线器。SignalR Hub未连接
这里是我下面的代码:
<!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#集线器:
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启动类:
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();
}
}
}
当我通过Visual Studio 2013运行此,我点击发送按钮,并没有警报出现告诉我,我已经按下了按钮。另外,当我稍后单击显示按钮时,messageArr数组仍为空。我可以改变什么使其实际工作?
答
好了,我不知道怎么样,但我似乎有固定我的代码
<!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>
如果任何人都可以向我解释如何固定我的问题,这将是有益的。