【问题标题】:How to send message from server to client using SignalR?如何使用 SignalR 从服务器向客户端发送消息?
【发布时间】:2021-11-26 13:23:36
【问题描述】:

如何使用 SignalR 从服务器向客户端发送内容? 我创建了如下发送方法:

    public async void SendData(string data)
    {
       await Clients.All.SendAsync("ReceiveMessage", data);
    }

我的javascript:

"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/somemsg").build();


connection.on("ReceiveMessage", function (message) {
    var li = document.createElement("li");
    document.getElementById("responeHistory").appendChild(li);
    li.textContent = `${message}`;
});

在我的情况下,我收到客户端“对象引用未设置为对象实例”的错误。

【问题讨论】:

  • 我你的连接至少连接了吗?你可以在 SendData() 中设置断点吗?如果是这样,它是否命中了方法?

标签: c# asp.net-core razor-pages


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-18
    • 2013-06-24
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多