gpt4 book ai didi

c# - SignalR 演示不工作

转载 作者:太空狗 更新时间:2023-10-29 20:37:01 26 4
gpt4 key购买 nike

我正在学习这个关于启动 SignalR 项目的快速教程,在我的 Hub 类中,我有以下内容

using Microsoft.AspNet.SignalR.Hubs;

namespace MvcApplication8.Hubs
{
public class ChatHub : Hub
{
public void BroadcastMessage(string message)
{
Clients.writeMessage(message);
}
}
}

客户端代码:

    $(document).ready(function () {
var chat = $.connection.chatHub;
chat.writeMessage = function(msg) {
$("#messages").append("<li>" + msg + "</li>");
};
$("#buttonSubmit").click(function () {
chat.broadcastMessage($("#txtInput").val());
});
$.connection.hub.start();
});

但是我收到一个编译时错误:

'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' does not contain a definition for 'writeMessage' and no extension method 'writeMessage' accepting a first argument of type 'Microsoft.AspNet.SignalR.Hubs.HubConnectionContext' could be found (are you missing a using directive or an assembly reference?)

哪里出了问题?

我从 http://nuget.org/packages/microsoft.aspnet.signalr 将 SignalR 安装到我的项目中使用包管理器控制台“Install-Package Microsoft.AspNet.SignalR -Pre”

最佳答案

您是否创建了相应的客户端功能?

<script type="text/javascript">
$(function () {
var chat = $.connection.chat;
chat.writeMessage = function (message) {
$('#messages').append('<li>' + message + '</li>');
};
$.connection.hub.start();
});
</script>

关于c# - SignalR 演示不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13607028/

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