微信小程序与AspNetCore SignalR聊天实例代码
作者:编程梦 时间:2022-12-21 01:29:06
微信小程序与aspnetcore signalr实例
本文不对小程序与signalr做任何介绍,默认读者已经掌握
aspnetcore Signalr文档
小程序文档
写在之前
SignalR没有提供小程序使用的客户端js,所以本人参考signlar.js写了小程序版signalr-client.js 代码开源,地址 https://github.com/liangshiw/SignalRMiniProgram-Client
先上效果图
开始编码
首先需要创建一个aspnetcore的mvc项目,创建完成后我们需要安装signalr的包
Install-Package Microsoft.AspNetCore.SignalR
现在就可以创建hub集线器了,首先定义一个类来描述已在线的用户,它需要头像和姓名
public class OnlineClient
{
public string NickName { get; set; }
public string Avatar { get; set; }
}
接下来我们在连接创建时,把当前用户做为在线用户添加到字典中,向该用户发送加入成功的系统消息。并且同时向其他的用户发送系统消息
public override async Task OnConnectedAsync()
{
var http = Context.GetHttpContext();
var client = new OnlineClient()
{
NickName = http.Request.Query["nickName"],
Avatar = http.Request.Query["avatar"]
};
lock (SyncObj)
{
OnlineClients[Context.ConnectionId] = client;
}
await base.OnConnectedAsync();
await Groups.AddToGroupAsync(Context.ConnectionId, ChatName);
await Clients.GroupExcept(ChatName, new[] { Context.ConnectionId }).SendAsync("system", $"用户{client.NickName}加入了群聊");
await Clients.Client(Context.ConnectionId).SendAsync("system", $"成功加入{ChatName}");
}
同样在用户断开连接时做离线处理
public override async Task OnDisconnectedAsync(Exception exception)
{
await base.OnDisconnectedAsync(exception);
bool isRemoved;
OnlineClient client;
lock (SyncObj)
{
isRemoved = OnlineClients.TryRemove(Context.ConnectionId, out client);
}
await Groups.RemoveFromGroupAsync(Context.ConnectionId, ChatName);
if (isRemoved)
{
await Clients.GroupExcept(ChatName, new[] { Context.ConnectionId }).SendAsync("system", $"用户{client.NickName}退出了群聊");
}
}
下面就只有一个简单的发送消息方法了,首先查看当前用户是否在线并做相应处理,如果在线就把当前用户的消息和头像姓名一起发送给组中的其他客户端
public async Task SendMessage(string msg)
{
var client = OnlineClients.Where(x => x.Key == Context.ConnectionId).Select(x=>x.Value).FirstOrDefault();
if (client == null)
{
await Clients.Client(Context.ConnectionId).SendAsync("system", "您已不在聊天室,请重新加入");
}
else
{
await Clients.GroupExcept(ChatName, new[] { Context.ConnectionId }).SendAsync("receive", new { msg, nickName = client.NickName, avatar = client.Avatar });
}
}
在小程序中,我们需要在页面加载事件中创建与signalr的连接,并且注册system系统消息与receive用户消息两个方法以接收服务端发来的消息
onLoad: function (options) {
this.hubConnect = new Hub.HubConnection();
this.hubConnect.start("https://chat.jingshonline.net/chat", { nickName: app.globalData.userInfo.nickName, avatar: app.globalData.userInfo.avatarUrl });
this.hubConnect.onOpen = res => {
console.log("成功开启连接")
}
this.hubConnect.on("system", res => {
wx.showModal({
title: '系统消息',
content: res,
})
})
this.hubConnect.on("receive", res => {
centendata.push({
content: res.msg,
time: new Date().toLocaleString(),
head_owner: res.avatar,
is_show_right: 0
});
this.setData({
centendata: centendata
})
})
}
同样在页面销毁时应断开与signalr服务器的连接
onUnload: function () {
this.hubConnect.close({ reason: "退出" })
}
发送方法也非常简单,只需要调用sendMessage方法并把用户输入的消息传入就大功告成了,其它就是页面上的处理了
this.hubConnect.send("sendMessage",message);
完整的代码请去github https://github.com/liangshiw/SignalRMiniProgram-Client/tree/master/sample
需要注意的是在打开小程序代码时,请修改project.config.json文件中的appid。
总结
以上所述是小编给大家介绍的微信小程序与AspNetCore SignalR聊天实例代码网站的支持!
来源:https://www.cnblogs.com/LiangSW/p/9415246.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java集合去重导致的线上问题
![](https://img.aspxhome.com/file/2023/7/65627_0s.webp)
hibernate-validator改进校验框架validator v0.4使用
spring cloud gateway请求跨域问题解决方案
Java实现部门员工管理
![](https://img.aspxhome.com/file/2023/4/81944_0s.jpg)
Android仿新浪微博分页管理界面(3)
![](https://img.aspxhome.com/file/2023/0/85450_0s.jpg)
一文搞懂Spring中的注解与反射
![](https://img.aspxhome.com/file/2023/8/58758_0s.jpg)
SpringBoot项目中使用Mockito的示例代码
![](https://img.aspxhome.com/file/2023/3/62373_0s.png)
java compare compareTo方法区别详解
Java反射通过Getter方法获取对象VO的属性值过程解析
Spring Boot 静态资源处理方式
![](https://img.aspxhome.com/file/2023/5/62535_0s.png)
java动态线程池的简单实现思路
Java持久化框架Hibernate与Mybatis优劣及选择详解
解决SpringBoot运行Test时报错:SpringBoot Unable to find
![](https://img.aspxhome.com/file/2023/9/79899_0s.png)
Android 消息机制问题总结
![](https://img.aspxhome.com/file/2023/5/84715_0s.jpg)
Java中常见的编码集问题总结
![](https://img.aspxhome.com/file/2023/5/60115_0s.png)
Spring activiti如何实现指定任务处理者
Maven配置文件pom.xml详解
Java中静态类型检查是如何进行的实例思路详解
在 Ubuntu Linux 上安装 Oracle Java 14的方法
Java ShutdownHook原理详解
![](https://img.aspxhome.com/file/2023/7/58457_0s.png)