【问题标题】:SignalR core - Attempting to pass clientId to Hub, can't figure out how to receiveSignalR 核心 - 尝试将 clientId 传递给 Hub,无法弄清楚如何接收
【发布时间】:2020-03-03 01:13:21
【问题描述】:

只需通过集线器将 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());
});

【问题讨论】:

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


    【解决方案1】:

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

     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

    【讨论】:

    • 不幸的是,当我将 ClientId 添加到上面时,它不再找到集线器,因此根本不会触发。
    • @TimCadieux 你是如何在 startup.cs 中配置你的信号器的?
    • 添加了启动行
    猜你喜欢
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-16
    • 2021-04-08
    • 2017-09-28
    • 2017-05-08
    • 1970-01-01
    相关资源
    最近更新 更多