Javascript 信号器核心-试图将客户端ID传递给集线器,can';我不知道如何接收
只需要通过hub将clientId传递给我的查询,但是我还没有找到一个合适的例子来说明如何传递和接收参数。我已尝试将clientId添加到ProductCountEventReceiverService,但随后中心停止响应 我的代码Javascript 信号器核心-试图将客户端ID传递给集线器,can';我不知道如何接收,javascript,c#,node.js,asp.net-core,signalr,Javascript,C#,Node.js,Asp.net Core,Signalr,只需要通过hub将clientId传递给我的查询,但是我还没有找到一个合适的例子来说明如何传递和接收参数。我已尝试将clientId添加到ProductCountEventReceiverService,但随后中心停止响应 我的代码 public interface IEventHub { Task ProductCountSendNoticeEventToClient(string message, string clientId); } public class EventHub :
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;
}
}
将消息从服务器发送到客户端
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());
});
您可以查看它,以便将消息从服务器发送到客户端
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());
});
您可以查看它不幸的是,当我将ClientId添加到上面时,它不再找到集线器,因此根本无法启动。@TimCadieux您是如何在startup.cs中配置信号器的?幸运的是,当我将ClientId添加到上面时,添加了启动行,它再也找不到集线器了,所以根本就不会触发。@TimCadieux您是如何在startup.cs中配置您的信号器的?添加了启动行