填坑避雷真君之——.NET5 + VUE使用SinalR实现前后端通讯
2021-01-04 19:29
标签:nbsp info orm fun ica efault 单点 高级功能 订阅 OK,前端结束
https://www.cnblogs.com/laozhang-is-phi/p/netcore-vue-signalr.html#tbCommentBody https://www.cnblogs.com/cgyqu/p/9563193.html 填坑避雷真君之——.NET5 + VUE使用SinalR实现前后端通讯 标签:nbsp info orm fun ica efault 单点 高级功能 订阅 原文地址:https://www.cnblogs.com/eddy777/p/14209938.htmlInstall-Package Microsoft.AspNetCore.SignalR
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace SignalRChat.Hubs
{
public class ChatHub : Hub
{
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
}
using SignalRChat.Hubs;
namespace SignalRChat
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseEndpoints(endpoints =>
{
//官网原Demo
//endpoints.MapHub
public class SignalRController
{
IHubContext
npm install @aspnet/signalr
import * as signalR from ‘@aspnet/signalr‘
created() {
// 1、创建连接对象
this.connection = new signalR.HubConnectionBuilder(
// 配置通道路由
.withUrl(‘/api/chatHub‘)
// 日志信息
.configureLogging(signalR.LogLevel.Information)
.build()
// 2、开始通讯
this.connection.start().then(function () {
console.log(‘连接成功!‘)
}).catch(function (err) {
console.log(‘连接失败!‘ + err)
});
// 3、订阅接收,ReceiveMessage为后端发送消息时写名称,必须一一对应
this.connection.on(‘ReceiveMessage‘, function (user, message) {
console.log(`成功接收消息!user:${user},message:${message}}}`)
// 接收到消息后你想做的事就写在这里
});
},
methods: {
call(){
// 4、呼叫后端
this.connection.invoke(‘ReceiveMessage‘, ‘张三‘,‘三儿,你还好吗?‘).catch(function (err) {
return console.error(err);
});
}
}
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
文章标题:填坑避雷真君之——.NET5 + VUE使用SinalR实现前后端通讯
文章链接:http://soscw.com/index.php/essay/40109.html