• 二、Signalr WebApi客服


    一、搭建环境

    (安装redis服务)

     更改端口0.0.0.0,保存并重启redis(记得宝塔开启安全)

     访问测试

    本地搭建的redis

     链接测试

     二、项目搭建 

    参考

     1、搭建项目(直接项目-不包含MVC以及API)

     项目结构

    但是需要访问(所以还需要添加控制器Api的模式)选择Api

    添加类库一个专门管理SignalR

     

     2、中要signalr包引用

    首先先用NuGet包下载SignalR包:

    Microsoft.AspNetCore.SignalR

    安装过后,需要得就是SignalR的JS文件,这里可以采用npm命令进行安装

    npm install @aspnet/signalr

    根据情景我不需要通过NuGet包下载 直接创建ChatHub.cs

    using Microsoft.AspNetCore.SignalR;
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SignalR
    {
        public class ChatHub : Hub
        {
            public async Task SendMessage(string user, string message)
            {
                await Clients.All.SendAsync("ReceiveMessage", user, message);
            }
        }
    }

     3、更改配置文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using SignalR;
    
    namespace Ban
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
                services.AddSignalR();//1、添加服务
            }
    
            // 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.UseCors(builder =>
                {
                    builder.SetIsOriginAllowed(origin => true)
                        .AllowAnyHeader()
                        .WithMethods("GET", "POST")
                        .AllowCredentials();
                });
    
                app.UseSignalR(routes =>
                {
                    routes.MapHub<ChatHub>("/SignalR");     //可以多个map  
                });
    
                app.UseMvc();
            }
        }
    }

     4、预览

    注意:

    (vs2017 自带升级修改添加没有关于.net core sdk2.2的包,只能通过下载sdk2.2安装 ,2019可以有) 需要关闭所有vs,重新打开。

    扩展 .net core 2.2 

    https://dotnet.microsoft.com/download/visual-studio-sdks?utm_source=getdotnetsdk&utm_medium=referral

    点到为止
  • 相关阅读:
    开启进程
    操作系统
    多线程(进程)目录
    网络编程-基于UDP协议套接字
    网络编程-文件传输
    EXt js 学习笔记总结
    Sencha Toucha 2.1 文件上传
    Sencha Touch 2.1学习图表Chart概述
    Sencha Touch 2.1 Chart属性中文解释
    Ext.Ajax.request方法 参数
  • 原文地址:https://www.cnblogs.com/fger/p/11880292.html
Copyright © 2020-2023  润新知