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

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

我的创业
//下面的SignalR
添加等等等等,因为堆栈溢出不会让我提交,这很烦人
等等等等等等等等等等

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

参考方案

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

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

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

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

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

您可以查看它here

Javascript-从当前网址中删除查询字符串 - javascript

单击提交按钮后,我需要从网址中删除查询字符串值。我可以用jQuery做到这一点吗?当前网址:siteUrl/page.php?key=value 页面提交后:siteUrl/page.php 实际上,我已经从另一个带有查询字符串的页面着陆到当前页面。我需要在页面首次加载时查询字符串值以预填充一些详细信息。但是,一旦我提交了表格,我就需要删除查询字符串值。我已…

Mongo汇总 - javascript

我的收藏中有以下文件{ "_id": ObjectId("54490b8104f7142f22ecc97f"), "title": "Sample1", "slug": "samplenews", "cat": …

JavaScript中的字符串评估函数 - javascript

            JavaScript中是否有任何内置函数,例如Python中的eval内置函数?注意:eval函数将方程式作为字符串并返回结果。例如,假设变量x为2,则eval("2x+5")返回9。 参考方案 是的,JavaScript中也有eval函数。此外,该声明应有效用于评估,即eval("2*x+5"…

如何在JQuery中操作JSONArray - javascript

我有一个php函数,它以JSON返回此代码{"0":{"title":"Dans l\u2019appartement"},"1":{"title":"A l\u2019a\u00e9roport - D\u00e9part de B\u00e9at…

在两个值之间匹配并返回正则表达式 - javascript

我正在尝试使用正则表达式从字符串中获取值,该值是tt="和"&之间的文本的值因此,例如,"tt="Value"&"我只想从中得到单词"Value"。到目前为止,我已经有了:/tt=.*&/这给了我"tt=Value"&,然后,要…