当前位置:网站首页>【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通信方法的补充,详细用法请参考视频~感谢~
边栏推荐
- 组态王如何利用无线Host-Link通信模块远程采集PLC数据?
- Collation of common array functions
- 详解异步任务:函数计算的任务触发去重
- Experiment 5 constructor and destructor
- What are the software development processes of the visitor push mall?
- C语言练习:ESP32 BLE低功耗蓝牙服务端数据打包和客户端数据解析
- WPS标题段前间距设置无效解决方案
- [node] common methods of path module
- QApplication a (argc, argv) and exec() in the main function of QT getting started
- 点云库pcl从入门到精通学习记录 第八章
猜你喜欢

IP编址概述
![[day 5 of JUC learning] reference atomic classes and attribute modifiers](/img/e5/3d39d34d8c423ec71da59f9b418f5a.png)
[day 5 of JUC learning] reference atomic classes and attribute modifiers
![[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

Flutter 使用本地图片

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

C语言练习:ESP32 BLE低功耗蓝牙服务端数据打包和客户端数据解析

月份选择器禁用当月以后的数据 包含当月

苹果手机wps如何改字体大小

Invalid spacing setting before WPS Title segment solution

Redis advanced - correspondence between object and code base
随机推荐
Openmmlab:ai CV framework
win10文件夹状态红叉表示的是什么
2022 618 notebook shopping guide
一、Flutter 入门学习写一简单客户端
苹果手机wps文件如何发送到qq邮箱
Anfulai embedded weekly report (issue 254): February 21, 2022 to February 27, 2022
Redis advanced - correspondence between object and code base
Construction environnementale 2
Share an open source, free and powerful video player library
wps表格怎么取消智能表格样式
点云库pcl从入门到精通学习记录 第八章
Is the o2o platform worth doing in 2022
Experiment 7 class construction and static member function
Jeecgboot 3.1.0 release, enterprise low code platform based on code generator
如何优化PlantUML流程图(时序图)
Seven trends in test automation that need attention
Point cloud library PCL from introduction to mastery learning records Chapter 8
JS——防止自动恢复页面位置
Enterprise wechat H5_ Integrated message decryption class, message push get and post callback processing
Devops landing practice drip and pit stepping records - (1)