zoukankan      html  css  js  c++  java
  • NetCore 下集成SignalR并进行分组处理

    Tips:

            1、注意跟普通版Net.MVC的前端处理方式不一样,以前可以connection.start()后直接done里面再做逻辑处理,现在不行了

        建议做法是在具体的业务Hub里重写OnConnectedAsync事件,来为当前连接ID做分组等处理

            2、目前版本只支持了websocket协议,还不支持Comet长连接方式

    后台代码:

        public class MessengerHub : Hub
        {
            private readonly IHttpContextAccessor _accessor;
            public MessengerHub(IHttpContextAccessor accessor)
            {
                _accessor = accessor;
            }
    
            //连上之后初始化Group
            public override async Task OnConnectedAsync()
            {
                var connectionId = Context.ConnectionId;
                var userInfo = _accessor.HttpContext.User.Identities.First(u => u.IsAuthenticated);
                await Task.Run(() =>
                {
                    this.Groups.AddAsync(Context.ConnectionId, _accessor.HttpContext.User.Identity.Name);
                });
            }
    
            //给指定Group客户端发送消息
            public async Task SendToMessage(string sendTo, string title, string content, int duration = 3000)
            {
                var connectionId = Context.ConnectionId;
                await Task.Run(() =>
                {
                    sendTo.Split(',').ToList().ForEach(t =>
                    {
                        this.Clients.Group(t).InvokeAsync("SendToMessage", "", title, content, duration);
                    });
                });
            }
        }
    

    前端代码需要引入signalr-client.js,可以在Bower包里添加或者在Github上找下

    前端关键代码:

    //消息处理
    let transportType = signalR.TransportType.WebSockets;
    let http = new signalR.HttpConnection(`http://${document.location.host}/messengerHub`, { transport: transportType });
    let connection = new signalR.HubConnection(http);
    connection.start();
    
    //setTimeout(connection.invoke('AddGroup'), 5000);
    connection.on('SendToMessage', (sendTo, title, content, duration) => {
        layer.open({
            type: 1
            , time: duration
            , title: title
            , offset: 'rb'//具体配置参考:http://www.layui.com/doc/modules/layer.html#offset
            //, id: 'LAY_demo_rb' //防止重复弹出
            , content: '<div id="MessageTips">' + content + '</div>'
        });
    });

    注意这个地方注释掉的connection.invoke('AddGroup'),这个地方不能在前台JS里面做分组初始化

    前台JS现在也不支持Done方法回调,所以写在了后台OnConnectedAsync方法里

    客户端发送方法跟以前的版本不一样:connection.invoke('SendToMessage','Admin','标题','消息提示',2000);

  • 相关阅读:
    OSX安装nginx和rtmp模块(rtmp直播服务器搭建)
    用runtime来重写Coder和deCode方法 归档解档的时候使用
    Homebrew安装卸载
    Cannot create a new pixel buffer adaptor with an asset writer input that has already started writing'
    OSX下面用ffmpeg抓取桌面以及摄像头推流进行直播
    让nginx支持HLS
    iOS 字典转json字符串
    iOS 七牛多张图片上传
    iOS9UICollectionView自定义布局modifying attributes returned by UICollectionViewFlowLayout without copying them
    Xcode6 iOS7模拟器和Xcode7 iOS8模拟器离线下载
  • 原文地址:https://www.cnblogs.com/leeolevis/p/7883478.html
Copyright © 2011-2022 走看看