当前位置:网站首页>【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通信方法的补充,详细用法请参考视频~感谢~
边栏推荐
- 1、 Getting started with flutter learn to write a simple client
- Gin integrated graphic verification code
- 设计消息队列存储消息数据的 MySQL 表格
- Month selector disable data after the current month to include the current month
- Seven trends in test automation that need attention
- 手机wps如何压缩文件打包发送
- [pat (basic level) practice] - [simple simulation] 1076 WiFi password
- 详解异步任务:函数计算的任务触发去重
- Enterprise wechat H5_ Integrated message decryption class, message push get and post callback processing
- 多年测试菜鸟对黑盒测试的理解
猜你喜欢

Flink CDC + Hudi 海量数据入湖在顺丰的实践

1、 Getting started with flutter learn to write a simple client

Pre resolution and scope
![[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC](/img/62/e53a83f04a5eab7ae4db47e8d4f3e5.jpg)
[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC

Optimization method of win7 FPS

Xiaomu's interesting PWN

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

2022 Tibet's latest eight members (security officer) simulated test question bank and answers

What is bonded warehouse and what is the difference between them

ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
随机推荐
Pre resolution and scope
多年测试菜鸟对黑盒测试的理解
Experiment four categories and objects
点云库pcl从入门到精通学习记录 第八章
Investment analysis and prospect Trend Research Report of global and Chinese cyclopentanyl chloride industry 2022-2028
组态王如何利用无线Host-Link通信模块远程采集PLC数据?
Redis advanced - correspondence between object and code base
Pycharm file name taboo
2023 spring recruit | ant group middleware Intern Recruitment
Global and Chinese nonene market operation research and future prospect forecast report 2022 ~ 2028
Invalid spacing setting before WPS Title segment solution
Mysql database: introduction to database 𞓜 addition, deletion, modification and query
repeat_ L2-007 family property_ set
Wechat applet Chinese English conversion
Sorting out app startup process
"Failure" of the prospectus of Laowang: Laowang made its first dash for listing in Hong Kong, and the turnover rate continued to decline
1、 Getting started with flutter learn to write a simple client
OpenMMLab:AI CV框架
Jmeter性能测试场景的创建和运行
IP addressing overview