如何动态地添加集线器SignalR并有不同的范围 [英] How to dynamically add hub to SignalR and have different scopes

查看:161
本文介绍了如何动态地添加集线器SignalR并有不同的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立与小部件仪表盘(建为指令)。我想在以后的阶段必须动态地添加小工具的能力,但我只喜欢活动的部件(和集线器)来接收数据,因此,如果一个小部件是不活跃我不想枢纽注册

I'm attempting to build a Dashboard with widgets (built as directives). I'd like to at a later stage have the ability to dynamically add widgets, but I'd only like the active widgets (and hubs) to receive data, thus if a widget isn't active i don't want the hub to be registered.

,例如,对于使用的应用的用户的时间会有一个全球signalR上下文,以及页面特定的人,可以根据需要将要催生/破坏。

Eg for the duration of the user using the app there will be a global signalR context, as well as page specific ones, which will be spawned/destroyed as needed.

这是我最好的尝试ATM ... ...这是不工作

工厂

   (function () {
    'use strict';

    angular.module('app').factory('hubFactory', ['permissionSvc', 'common', hubFactory]);

    function hubFactory(permissionSvc, common) {

        var connection = [];

        return {
            context: function (name) {

                if (!connection[name]) {
                    var conn = $.connection;
                    conn.hub.url = common.serviceUrl + '/signalr';
                    conn.hub.start();

                    conn.prototype.addHub = function (hubName, options) {

                        // defaults
                        var opts = {
                            authorize: true
                        };

                        angular.extend(opts, options);

                        if (opts.authorize) {
                            permissionSvc.createPrefilter();
                        }

                        var hub = conn[hubName];

                        var run = function () {
                            hub.server.initialise();
                        };

                        return {
                            hub: hub,
                            run: run
                        };
                    };

                    connection[name] = conn;
                }

                return connection[name]();
            }
        };
    }
})();

部件指令控制器

 controller: function ($scope) {               

               var context = hubFactory.context('dashboard');

               var instance = context.addHub('agreementApprovalsHub');

               instance.hub.client.getAllUnapprovedAgreements = function (data) {

                    $scope.data = data;
                };

               instance.run();
            } 

下面需要在开始的完成方法被调用...但是,如果我想,启动在页面加载的连接,然后根据需要追加枢纽什么(或者是我想错了吗?)

The following needs to be called in the done method of the start... but what if i want to, start up the connection on page load, and then append hubs as needed (or is my thinking wrong?)

有各种各样的问题:

VAR运行=函数(){
                                  hub.server.initialise();
                              };

var run = function () { hub.server.initialise(); };

VS

VAR运行=函数(){conn.hub.start()。完成(功能(){
                                  hub.server.initialise(); });
                              };

var run = function () { conn.hub.start().done(function() { hub.server.initialise(); }); };

TBH,那感觉就像我在屠宰code,并且需要从头开始在这个阶段大概开始..
我对如何去这一切彻底糊涂了,如果它甚至有可能。

TBH, it feels like i'm butchering the code, and need to probably start from scratch at this stage.. I'm thoroughly confused on how to go about it all, and if its even possible.

推荐答案

我觉得我有一个更好的解决方案。

I think I have a more elegant solution.

服务

 app.factory("signalrFactory", function ($rootScope, $http, $timeout) {
    var factory = {};
    factory.connection = $.connection;

    var startDoneFunctions = [];
    var debounce;

    factory.start = function (done) {
        factory.connection.hub.stop();

        if (done) {
            if (startDoneFunctions.indexOf(done) == -1) {
                startDoneFunctions.push(done);
            }
        }
        if (debounce) $timeout.cancel(debounce);
        debounce = $timeout(function () {
            factory.connection.hub.start().done(function () {
                for (var x = 0; x < startDoneFunctions.length; x++) {
                    startDoneFunctions[x]();
                }
            });
        }, 100);
    };

    return factory;
});

使用

controller('customerSummary', function ($scope, signalrFactory) {
        $scope.customerHub = signalrFactory.connection.customerHub;
        $scope.customer;
        $scope.beingEditedText = "";



        $scope.customerHub.client.beingEdited = function (text) {
            $scope.beingEditedText = text;
        };
        $scope.customerHub.client.doneEditing = function () {
            $scope.beingEditedText = "";
        };

        $scope.customerHub.client.updateCustomer = function (customer) {
            $scope.customer = customer;
            $scope.$apply();
        };

        $scope.init = function (id) {
            signalrFactory.start(function () {
                $scope.customerHub.server.getCustomer(id);

            });
        };
    });

这是什么办法让需要而不必在引导添加每个集线器时添加。我现在能够从任何地方有不同的()方法进行。

What this way allows is to add when needed and not having to add each hub at bootstrap. I am now able to have different done() methods from anywhere.

这篇关于如何动态地添加集线器SignalR并有不同的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆