SignalR的简单使用(用户导出文件后给用户发送消息并且带上导出文件地址及名称)

关于SignalR
SignalR 简介

实际使用小案例
在平台中存在表格导出功能,表格导出功能使用NPOI导出,因此导出成功后需要给用户发送消息,告诉用户文件已经导出成功,并且弹出界面,该界面呈现出刚刚导出的一些文件,点击文件链接即可下载。如图所示
SignalR的简单使用(用户导出文件后给用户发送消息并且带上导出文件地址及名称)_第1张图片
关于Excel导出

  1. 新建ExcelHub类,用来处理文件导出的消息发送
    SignalR的简单使用(用户导出文件后给用户发送消息并且带上导出文件地址及名称)_第2张图片
    ExcelHub.cs
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WingSoft.SignalR
{
    /// 
    /// excel操作 通知
    /// 
    public class ExcelHub : Hub
    {
        public static ExcelHub Single = new ExcelHub();
        private static readonly IHubContext context = GlobalHost.ConnectionManager.GetHubContext<ExcelHub>();
        private readonly static List<UserClient> _connections = new List<UserClient>();

        /// 
        /// 发消息给指定平台的客户端
        /// 
        /// 
        /// 
        public static void sendMessage(int userId, string message)
        {
            if (_connections.Any())
            {
                try
                {
                    _connections.Where(o => o.UserId == userId).ToList().ForEach(t => context.Clients.Client(t.ConnectionId).broadcastMessage(t.ConnectionId, message));
                }
                catch (Exception)
                {
                }
            }
        }

        /// 
        /// 客户端连接之后发送此消息  用于绑定客户端
        /// 
        /// 用户id
        public void sendUser(int userId)
        {
            //这里便是将用户clientId和姓名联系起来
            _connections.Add(new UserClient { UserId = userId, ConnectionId = Context.ConnectionId });
        }

        /// 
        /// 客户端断开连接后调用
        /// 
        /// 
        /// 
        public override Task OnDisconnected(bool stopcalled)
        {
            var jk = (from u in _connections where u.ConnectionId == Context.ConnectionId select u).ToList();
            if (jk != null && jk.Any())
            {
                _connections.Remove(jk[0]);
            }
            return base.OnDisconnected(stopcalled);
        }
    }


    public class UserClient
    {
        public int UserId { get; set; }
        public string ConnectionId { get; set; }
    }
}

消息发送

 FileUrlAndName fileUrlAndName = new FileUrlAndName();
 fileUrlAndName.Url = ret.Message;
 fileUrlAndName.Name = excelNameSuffix;
 ExcelHub.sendMessage(userId, fileUrlAndNameString);

前端接收部分

<script type="text/javascript" src="~/Scripts/jquery.signalR-2.4.3.js"></script>
$(function () {
    excelHubConnect()
})
function excelHubConnect() {
            var con = $.hubConnection();
            var hub = con.createHubProxy("ExcelHub");
            hub.on('broadcastMessage', function (receiver, message) {
                var mdata = JSON.parse(message);
                let temphtml = "";
                temphtml += "
"; temphtml += "
"; temphtml += ""; temphtml += "
"
; temphtml += "" + mdata.Name + ""; temphtml += "
"
; $(".download-wrap .downt-tag").append(temphtml); $(".download-wrap").show(); $(".download-icon").show(); $(".download-icon").css("width", "400px"); $(".download-icon").css("right", "12px"); $("#showHideLoade").attr("src", "/Images/downloadHide.png"); isShow = false; }); con.start().done(function () { hub.invoke("sendUser", "@ViewBag.user.Id"); }); }

你可能感兴趣的:(后端,前端,SignalR,NPOI导出消息发送)