gpt4 book ai didi

javascript - SignalR 集线器未连接

转载 作者:行者123 更新时间:2023-11-28 05:48:53 25 4
gpt4 key购买 nike

我正在尝试创建 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# 中心:

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);
}
}
}

欧文创业类:

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>

如果有人可以向我解释这如何解决我的问题,那将会很有帮助。

关于javascript - SignalR 集线器未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38250698/

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