ASP.NET Core 2.1基于SignalR和Vue实时通讯

  1. 创建ASP.NET Core Web应用空项目模板程序(SDK版本.NET Core 2.1
  2. 在项目上右击 -> 添加 -> 新建文件夹 -> 名称命名为Hubs
  3. Hubs文件夹上右击 -> 添加 -> 类 -> 命名为ChatHub
  4. wwwroot文件夹上右击 -> 添加 -> 类 -> 命名为js
  5. 在项目上右击 -> 添加 -> 添加客户端库
    a. 提供程序选择unpkg
    b. 输入@aspnet/signalr@1.1.4
    c. 选中选择特定文件
    d. 勾选signalr.jssignalr.js.mapsignalr.min.jssignalr.min.js.map
    e. 目标位置改为wwwroot/js/
  6. 在项目上右击 -> 添加 -> 添加客户端库
    a. 提供程序选择cdnjs
    b. 输入vue@2.6.14
    c. 选中选择特定文件
    d. 勾选vue.jsvue.min.js
    e. 目标位置改为wwwroot/js/
  7. js文件夹上右击 -> 添加 -> 新建项 -> 选择JavaScript 文件 -> 名称改为site.js
"use strict";

var app = new Vue({
    el: '#app',
    data: {
        message: ''
    },
    created() {
        // 监听事件
        this.$on('receiveMessage', (msg) => {
            this.message = msg;
        })
    },
})

var hub = new signalR.HubConnectionBuilder().withUrl("/chatHub").build()

hub.start().then(function () {
    // 使用定时器请求数据
    setInterval(function () {
        hub.invoke("Send").catch(function (err) {
            return console.error(err.toString());
        });
    }, 1000);
}).catch(function (err) {
    return console.error(err.toString());
});

hub.on("Receive", function (msg) {
    // 使用事件触发
    app.$emit("receiveMessage", msg);
});
  1. wwwroot文件夹上右击 -> 添加 -> 新建项 -> 选择HTML 页 -> 名称改为Index.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>Home Page</title>
</head>
<body>
    <div id="app">
        {{ message }}
    </div>
    <script src="./js/vue/vue.min.js"></script>
    <script src="./js/dist/browser/signalr.min.js"></script>
    <script src="./js/site.js"></script>
</body>
</html>
  1. 修改StartUp.cs
public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(options => options.AddPolicy("CorsPolicy",
        builder =>
        {
            builder.AllowAnyMethod().AllowAnyHeader()
                    .AllowCredentials();
        }));

        services.AddSignalR();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        // 允许跨域请求及使用静态文件作为请求路径服务
        app.UseHttpsRedirection()
            .UseDefaultFiles()
            .UseCors("CorsPolicy")
            .UseSignalR(route =>
        {
            route.MapHub<ChatHub>("/chathub");
        }).UseStaticFiles();

        //app.Run(async (context) =>
        //{
        //    await context.Response.WriteAsync("Hello World!");
        //});
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值