[英]Call specific client from SignalR server (Asp.Net Core)
I'am using Asp.Net Core SignalR in C# with a Javascript client and I'am trying to call a client method from the Server.我在 C# 中使用 Asp.Net Core SignalR 和 Javascript 客户端,我正在尝试从服务器调用客户端方法。 Now this is done via a Controller method, which is called from an external source.现在这是通过从外部源调用的 Controller 方法完成的。 I invoke the Hub context in the Controller class, which allows me to use the hub context and using this I try to make a call to the specific client using a connection ID, which fails to work with no error thrown.我在 Controller class 中调用集线器上下文,它允许我使用集线器上下文,并使用它尝试使用连接 ID 调用特定客户端,但它无法正常工作,没有抛出错误。 I will show you what code I have so far.我会告诉你我到目前为止有什么代码。
This is my Hub class, which merely is used to return the connection ID to the client when requested.这是我的集线器 class,它仅用于在请求时将连接 ID 返回给客户端。
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
namespace SignalR1
{
public class TestHub : Hub
{
public string GetConnectionId() => Context.ConnectionId;
}
}
This is my JS client code to make the connection to the hub.这是我的 JS 客户端代码,用于连接到集线器。
var connection;
var connId;
async function signalRStart() {
connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:44354/testHub")
.configureLogging(signalR.LogLevel.Information)
.build();
connection.on("ClientFunction", (param) => {
alert("ClientFunction got (" + param + ")");
});
connection.on("testHubToHothUpdate", () => {
console.log("I've been called by the server!");
});
connection.onclose(async () => {
await start();
});
// Start the connection.
await start();
}
async function start() {
try {
await connection.start();
console.log("SignalR Connected.");
var connId = await connection.invoke("GetConnectionId");
signalRPost(connId); // Business logic method
} catch (err) {
console.log(err);
setTimeout(start, 5000);
}
};
You can see it makes a connection to the hub and then provides 2 methods at the client end "ClientFunction" and "testHubToHothUpdate".您可以看到它与集线器建立连接,然后在客户端“ClientFunction”和“testHubToHothUpdate”提供 2 个方法。 connId is used to store the value of the connection ID once it is retrieved from the server. connId 用于存储从服务器检索到的连接 ID 的值。
This is the controller class.这是 controller class。
using Microsoft.AspNetCore.SignalR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace SignalR1.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly IHubContext<HothHub> _hubContext;
public ValuesController(IHubContext<HothHub> hubContext)
{
_hubContext = hubContext;
}
[HttpPost("{id}")] // POST /api/values/xyz
public async Task<IActionResult> PostAsync(string id)
{
Trace.TraceInformation("update Post()");
// Make a call to the client based on the connection ID and call client method
await _hubContext.Clients.Client(id).SendAsync("testHubToHothUpdate");
return Ok();
}
}
}
I've used a controller class because persistent connections is not available for use in Asp.Net Core .我使用了 controller class 因为持久连接不适用于Asp.Net Core 。 Using Clients.All.SendAsync() works fine as the client method is hit but I need to be able to call a specific client based on the connection ID.使用 Clients.All.SendAsync() 可以正常工作,因为客户端方法被命中,但我需要能够根据连接 ID 调用特定的客户端。 The id passed into this Post method is correct and matches up with the live connection ID in the client (I have run several tests for this).传递给这个 Post 方法的 id 是正确的,并且与客户端中的实时连接 ID 匹配(我已经为此运行了几个测试)。 Is using Client(id) not allowed any more or is there anything I need to make sure of to get this working?是否不再允许使用 Client(id) 或者我需要确保什么才能使其正常工作? Any help is appreciated on this, thank you!对此的任何帮助表示赞赏,谢谢!
PS I should I'm not using user authentication on the client side so sending to a specific user id is not going to be possible. PS我应该不在客户端使用用户身份验证,因此无法发送到特定的用户ID。 I'm trying to send to a specific connection id instead.我正在尝试发送到特定的连接 ID。
It looks like you have two different hubs.看起来您有两个不同的集线器。 You connect to TestHub
:您连接到TestHub
:
public class TestHub : Hub
{
public string GetConnectionId() => Context.ConnectionId;
}
...
connection = new signalR.HubConnectionBuilder()
.withUrl("https://localhost:44354/testHub")
...
but then you are trying to send to client using HothHub
:但随后您尝试使用HothHub
发送给客户端:
private readonly IHubContext<HothHub> _hubContext;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.