创建webapi项目,只是搭配服务用,什么三层mvc都不弄了,默认的模板直接用就好。

简单分析下,采用signalr通信来传递mac地址,所以先安装个signalr的包(如果简单操作的话可以不装最新的,微软自带一个,不过好像是弃用的)

using Microsoft.AspNetCore.SignalR;

创建一个继承HUB的辅助类,主要功能就是服务端和客户端通信

补充一下HUB类怎么写

 public class SignalRHub : Hub
    {
        private readonly IHubContext<SignalRHub> _hubContext;
        private readonly List<string> connectionIDs;
        private readonly IMemoryCache _memoryCache;
        private int num;

        public SignalRHub(IHubContext<SignalRHub> hubContext, IMemoryCache memoryCache)
        {
            connectionIDs = new List<string>();
            _hubContext = hubContext;
            _memoryCache = memoryCache;
        }


        #regionpublic void Mac1(string macMsg)
        {
            num++;
            if (!string.IsNullOrEmpty(macMsg) || num == connectionIDs.Count)
            {
                num = 0;
                _memoryCache.Set("mac", macMsg); // 存储mac值到内存缓存中
            }
        }
        #endregion


        #region/// <summary>
        /// 客户端连接之后立马执行
        /// </summary>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {
            // 保存连接的客户端到上下文中
            connectionIDs.Add(Context.ConnectionId);
            await Clients.Caller.SendAsync("Connected", Context.ConnectionId);
            //添加到组
            await Groups.AddToGroupAsync(Context.ConnectionId, "Mac");
            await base.OnConnectedAsync();
        }

        public async Task<string> SendIP(string ip)
        {
            //await Clients.All.SendAsync("匹配IP", ip);//给所有的客户端发送IP进行匹配
            await _hubContext.Clients.Group("Mac").SendAsync("匹配IP", ip);
            await Task.Delay(2000);
            string mac = _memoryCache.Get<string>("mac"); // 从内存缓存中读取mac值
            return mac;
        }
        #endregion
    }
View Code

 

 

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

namespace MacService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MacSignalRController : ControllerBase
    {
        /// <summary>
        /// 辅助类注入进来
        /// </summary>
        private readonly SignalRHub _signalRHub;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="SignalRHub"></param>
        public MacSignalRController(SignalRHub SignalRHub)
        {
            _signalRHub = SignalRHub;
        }
        /// <summary>
        /// POST请求,传入ip比对客户端ip拿到mac地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<string> SendMessage([FromBody] string ip)
        {
            return await _signalRHub.SendIP(ip);
        }
    }
}
View Code

 

 写一个接口,给前端浏览器用,传入ip和安装了服务的电脑的ip对比,拿到对应的mac地址

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;

namespace MacService.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MacSignalRController : ControllerBase
    {
        /// <summary>
        /// 辅助类注入进来
        /// </summary>
        private readonly SignalRHub _signalRHub;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="SignalRHub"></param>
        public MacSignalRController(SignalRHub SignalRHub)
        {
            _signalRHub = SignalRHub;
        }
        /// <summary>
        /// POST请求,传入ip比对客户端ip拿到mac地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<string> SendMessage([FromBody] string ip)
        {
            return await _signalRHub.SendIP(ip);
        }
    }
}
View Code

 

因为有用到依赖注入,所以要在Program.cs文件进行注册

1.signalr   2.SignalRHub辅助类   3.MemoryCache 

    builder.Services.AddSignalR();
    builder.Services.AddScoped<SignalRHub>();
    builder.Services.AddMemoryCache();

 

还有要给signalr暴露出来让客户端可以连接,好像是这样解释吧,不知道怎么解释

     app.MapHub<SignalRHub>("/signalrhub");    

 

然后你的ip+端口+signalrhub  类似与:https://123.321.456.567:1234/signalrhub  这样,如下图则为成功

 


 

现在弄windows服务,这个博客园有大佬写了,我也是看的那个,这里贴个链接

使用.NET Core创建Windows服务详细步骤 - willingtolove - 博客园 (cnblogs.com)

 安装和卸载的命令发一下

@echo off 
chcp 65001 @title 安装windows服务 @echo off echo
= 安装服务! @echo off @sc create MacClientAssist binPath= "%~dp0MacClientAssist.exe" echo= 启动服务! @echo off @sc start MacClientAssist @echo off echo= 配置服务! @echo off @sc config MacClientAssist start= AUTO @echo off echo= 成功安装、启动、配置服务! @pause
@echo off
chcp 65001 @title 卸载Windows服务 @echo off echo 正在停止服务... @echo off @sc stop MacClientAssist echo 正在卸载服务... @echo off @sc delete MacClientAssist echo 服务已成功卸载。 @pause

 

 

 

 

 

 

 

 

 

 

 

 

 

 

           

 

posted on 2023-07-02 05:29  尝尝手指  阅读(57)  评论(0编辑  收藏  举报