当前位置:网站首页>【SignalR全套系列】之在.Net6中实现SignalR分组通信
【SignalR全套系列】之在.Net6中实现SignalR分组通信
2022-06-12 00:36:00 【dotNET跨平台】
微信公众号:趣编程ACE
关注可了解更多的.NET日常实战开发技巧,如需源码 请公众号后台留言 源码;
[如果觉得本公众号对您有帮助,欢迎关注]
前文回顾
【SignalR全套系列】之在.Net6中实SignalR通信SignalR中给客户端分组调用演示
服务端代码实现
1.Program.cs 文件编写
1// 捕获一个 /groups 节点 映射到 GroupHub 类里面
2app.MapHub<GroupHub>("/groups");2.自定义一个GroupHub类
1// GroupHub 类继承 Hub 类
2public class GroupHub : Hub
3 {
4 // 添加组 根据客户端的ConnectionId 添加到组名 为 testGroup 的组中去
5 public Task Join() => Groups.AddToGroupAsync(Context.ConnectionId, "testGroup");
6
7 // 从testGroup 去除对应的客户端
8 public Task Remove() => Groups.RemoveFromGroupAsync(Context.ConnectionId, "testGroup");
9
10 // 只有所在组的客户端可以接收到服务端发送的消息
11 public Task Message() => Clients
12 .Groups("testGroup")
13 .SendAsync("groupMessages", new TransData(Guid.NewGuid(), "属于 testGroup 才能接收到数据"));
14 }客户端代码实现
1 <script src="signalr.js"></script>
2 <script>
3 let connection = new signalR.HubConnectionBuilder()
4 .withUrl("/groups") // 调用 groups 路由
5 .build();
6
7 // groupMessages 钩子函数
8 connection.on('groupMessages', data => console.log("message", data));
9
10 // 和服务端建立连接 及其回调函数
11 connection.start().then(() => console.log("connected"));
12
13 // 调用Hub 中定义的方法
14 const join = () => connection.send('Join');
15 const remove = () => connection.send('Remove');
16 const messageMethod = () => connection.send('Message');
17
18
19 </script>以上便是对上文SignalR通信方法的补充,详细用法请参考视频~感谢~
边栏推荐
- Mathematical modeling experience ----- summary of three modeling
- Oracle uses Navicat tool to import and export data
- What is bonded warehouse and what is the difference between them
- JS——防止自动恢复页面位置
- ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
- Pre resolution and scope
- Collation of common array functions
- [academic related] it is suggested to collect. Which behaviors are academic misconduct?
- leetcodeSQL:614. Secondary followers
- Invalid spacing setting before WPS Title segment solution
猜你喜欢

gin解决跨域问题

2022 618 notebook shopping guide

Experiment 7 class construction and static member function

win10文件夹状态红叉表示的是什么
![[pat (basic level) practice] - [simple simulation] 1076 WiFi password](/img/e4/82bdc74dcaa9f397c81135da0c0097.jpg)
[pat (basic level) practice] - [simple simulation] 1076 WiFi password

Month selector disable data after the current month to include the current month

苹果手机wps文件如何发送到qq邮箱

ROS2之OpenCV基础代码对比foxy~galactic~humble

Exploration of qunar risk control safety products

1、 Getting started with flutter learn to write a simple client
随机推荐
730.Count Different Palindromic Subsequences
What are the software development processes of the visitor push mall?
Understanding of black box testing by novices after years of testing
環境搭建2
Flink CDC + Hudi 海量数据入湖在顺丰的实践
月份选择器禁用当月以后的数据 包含当月
Pre resolution and scope
关于接口测试的那些“难言之隐”
"Failure" of the prospectus of Laowang: Laowang made its first dash for listing in Hong Kong, and the turnover rate continued to decline
2022 edition of global and Chinese on silicon liquid crystal market supply and demand research and prospect Trend Forecast Report
Mathematical modeling experience ----- summary of three modeling
2021 inventory: Top 10 low code development platforms at home and abroad
WPS标题段前间距设置无效解决方案
win10系统pscs6如何卸载
详解异步任务:函数计算的任务触发去重
Web keyboard input method application development guide (2) -- keyboard events
Jmeter性能测试场景的创建和运行
Detailed explanation of merge sorting
How to make scripts executable anywhere
QApplication a (argc, argv) and exec() in the main function of QT getting started