当前位置:网站首页>【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通信方法的补充,详细用法请参考视频~感谢~
边栏推荐
- 苹果手机wps文件如何发送到qq邮箱
- [day 7 of JUC learning] reentrantlock and reentrantreadwritelock
- Experiment 6 constructor + copy construction
- JDBC exercises
- Motianlun domestic database salon | Xu Li: Alibaba cloud's native lindorm TSDB database drives the upgrading of industrial it & ot hyper integrated digital system
- 2022 618 notebook shopping guide
- Xiaomu's interesting PWN
- Tencent programmer roast: 1kW real estate +1kw stock +300w cash, ready to retire at the age of 35
- Mathematical modeling experience ----- summary of three modeling
- Breadth first search depth first search dynamic programming leetcode topic: delivering information
猜你喜欢

Microservice automation

App startup process: from clicking the application icon to the activity startup process
![[academic related] it is suggested to collect. Which behaviors are academic misconduct?](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[academic related] it is suggested to collect. Which behaviors are academic misconduct?

The latest report of Xinsi technology shows that 97% of applications have vulnerabilities

LabVIEW Arduino电子称重系统(项目篇—1)

How does Kingview use the wireless host link communication module to remotely collect PLC data?

ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session

Anfulai embedded weekly report (issue 254): February 21, 2022 to February 27, 2022

Experiment 5 constructor and destructor

How to send Apple phone WPS files to QQ mailbox
随机推荐
WPS标题段前间距设置无效解决方案
Flutter 使用本地图片
苹果手机wps如何改字体大小
repeat_ L2-009 red envelope grabbing_ sort
Environment construction 2
深度学习100例 | 第41天:语音识别 - PyTorch实现
ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session
DevOps落地实践点滴和踩坑记录-(1)
How to package and send compressed files for mobile WPS
Experiment 7 class construction and static member function
[case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC
What are the advantages of Tiktok applet
Water for a while
2022 edition of global and Chinese hexamethylene chloride industry dynamic research and investment prospect forecast report
Visitors push e-commerce express without tossing about personal payment codes
[academic related] it is suggested to collect. Which behaviors are academic misconduct?
JS -- prevent automatic recovery of page position
[node] common methods of path module
gin解决跨域问题
Exploration of qunar risk control safety products