[英]SignalR Hub not connecting
我正在嘗試創建一個SignalR應用程序,但我無法連接到我的集線器。
這是我的以下代碼:
<!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:
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>
如果有人能向我解釋這是如何解決我的問題會有所幫助的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.