ASP.NET Core SignalR 仅将消息路由到特定的客户端
创始人
2024-09-15 12:00:36
0

在ASP.NET Core SignalR中,可以通过使用组和连接来将消息路由到特定的客户端。下面是一个包含代码示例的解决方法:

  1. 在服务器端,首先定义一个Hub类,继承自Microsoft.AspNetCore.SignalR.Hub类:
using Microsoft.AspNetCore.SignalR;

public class MyHub : Hub
{
    public async Task SendMessageToClient(string userId, string message)
    {
        // 调用特定用户的客户端方法
        await Clients.User(userId).SendAsync("ReceiveMessage", message);
    }

    public async Task AddToGroup(string groupName)
    {
        // 将连接添加到组中
        await Groups.AddToGroupAsync(Context.ConnectionId, groupName);
    }

    public async Task RemoveFromGroup(string groupName)
    {
        // 将连接从组中移除
        await Groups.RemoveFromGroupAsync(Context.ConnectionId, groupName);
    }

    public override async Task OnConnectedAsync()
    {
        // 获取当前连接的用户ID
        string userId = Context.UserIdentifier;

        // 将连接添加到特定用户的组中
        await Groups.AddToGroupAsync(Context.ConnectionId, userId);

        await base.OnConnectedAsync();
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        // 获取当前连接的用户ID
        string userId = Context.UserIdentifier;

        // 将连接从特定用户的组中移除
        await Groups.RemoveFromGroupAsync(Context.ConnectionId, userId);

        await base.OnDisconnectedAsync(exception);
    }
}
  1. 在客户端,使用SignalR连接到服务器,并处理接收到的消息:
// 创建SignalR连接
var connection = new signalR.HubConnectionBuilder().withUrl("/myHub").build();

// 定义接收消息的客户端方法
connection.on("ReceiveMessage", function (message) {
    console.log("Received message: " + message);
});

// 启动连接
connection.start().then(function () {
    console.log("Connected to hub");

    // 将连接添加到特定组中
    connection.invoke("AddToGroup", "groupId").then(function () {
        console.log("Added to group");
    }).catch(function (err) {
        console.error(err);
    });
}).catch(function (err) {
    console.error(err);
});
  1. 在服务器端调用SendMessageToClient方法来发送消息到特定的客户端:
public class HomeController : Controller
{
    private readonly IHubContext _hubContext;

    public HomeController(IHubContext hubContext)
    {
        _hubContext = hubContext;
    }

    public async Task Index()
    {
        // 发送消息到特定用户的客户端
        await _hubContext.Clients.User("userId").SendAsync("ReceiveMessage", "Hello from server");

        return View();
    }
}

这样就可以将消息仅路由到特定的客户端。当客户端连接到服务器时,服务器会将连接添加到特定用户的组中,然后在服务器端调用SendMessageToClient方法时,使用特定用户的用户ID来调用对应的客户端方法。

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...