javascript - SignalR 核心 - 尝试将 clientId 传递给 Hub,无法弄清楚如何接收

标签 javascript c# node.js asp.net-core signalr

只需通过集线器将 clientId 传递给我的查询,但我无法找到如何传递和接收参数的正确示例。我尝试将 clientId 添加到 ProductCountEventReceiverService 但随后集线器停止响应。

我的代码

public interface IEventHub
{
    Task ProductCountSendNoticeEventToClient(string message, string clientId);
}
public class EventHub : Hub<IEventHub>
{
    // method for client to invoke
}
public class ProductCountEventReceiverService
{
    private readonly IHubContext<EventHub> _eventHub;

    private Timer _EventGenTimer;

    public ProductCountEventReceiverService(IHubContext<EventHub> eventHub,  string clientId)
    {
        _eventHub = eventHub;

        Init();
    }

    private void Init()
    {
        _EventGenTimer = new Timer(5 * 1000)
        {
            AutoReset = false
        };

        _EventGenTimer.Elapsed += EventGenTimer_Elapsed;
    }

    public void StartReceive()
    {
        _EventGenTimer.Start();
    }

    private void EventGenTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        _EventGenTimer.Enabled = false;

        var counts = Business.Product.GetCounts("837");  //Get the ClientID here

        _eventHub.Clients.All.SendAsync(nameof(IEventHub.ProductCountSendNoticeEventToClient),
            JsonConvert.SerializeObject(counts)).GetAwaiter().GetResult();

        _EventGenTimer.Enabled = true;
    }
}

我的创业 //下面的信号R 添加等等,因为堆栈溢出不会让我提交,这非常烦人 哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSignalR();
        services.AddSingleton<ProductCountEventReceiverService>();
    }

   public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseSignalR(routes =>
        {
            routes.MapHub<EventHub>("/eventHub");
        });
     }

我的 JavaScript

var connection = new signalR.HubConnectionBuilder()
    .withUrl("/eventHub?clientId=837")
    .build();

Object.defineProperty(WebSocket, 'OPEN', { value: 1 });
var clientId = $("#CurrentClientId").val();

connection.on("ProductCountSendNoticeEventToClient", clientId, function (message) {

    var result = $.parseJSON(message);
    //Do something here
});

connection.start().catch(function (err) {
    return console.error(err.toString());
});

最佳答案

从服务器向客户端发送消息

 public async Task SendMessage(string user, string message)
 {
   await Clients.All.SendAsync("ReceiveMessage", user, message);
 }

要从客户端接收消息,请使用connection.on

connection.on("ReceiveMessage", function (user, message) { }

要将消息从客户端发送到服务器,请使用connection.invoke

connection.invoke("SendMessage", user, message).catch(function (err) {
        return console.error(err.toString());
    });

您可以查看here

关于javascript - SignalR 核心 - 尝试将 clientId 传递给 Hub,无法弄清楚如何接收,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58732077/

相关文章:

node.js - CloudFront 中的 AWS Lambda@Edge 502 LambdaValidationError

javascript - 无法绑定(bind)到 '(ngModel',因为它不是 Angular 单元测试用例中 'input' 的已知属性

c# - 从LINQ查询结果中检索ID

C# - 将对象转换为接口(interface)

javascript - 如果我使用 Object.defineProperty() 定义 "name"属性,Error.prototype.toString() 将抛出 TypeError

node.js - Sequelize - 要定义外键,我应该使用引用还是 belongsTo?或两者?

javascript - 如何从 View 源 : page? 获取元素

c# - 部分 View 不会在 ajax.beginform 之后更新

javascript - Mongoose 只更新选定的字段或插入

c# - 生成json时类型无效?