我们如何确定启动SignalR扩展方法的组?

时间:2016-09-16 14:27:54

标签: javascript jquery vb.net signalr

我创建了一个Javascript function,使SignalR更加神奇:

//Initializable
function Initializable(params) {
    this.initialize = function (key, def, private) {
        if (def !== undefined) {
            (!!private ? params : this)[key] = (params[key] !== undefined) ? params[key] : def;
        }
    };
}
/*SignalR Updater*/
function SignalRUpdater(params) {
    Initializable.call(this, params);
    var self = this;
    this.initialize("RawHubs", [], true);
    this.initialize("RawGroups", {}, true);
    this.initialize("Connection", $.connection, true);
    this.initialize("Extend", {});
    this.Hubs = {};
    this.addHub = function (name, extend) {
        if (self.Hubs[name]) {
            return false;
        }
        self.Hubs[name] = params.Connection[name];
        self.Hubs[name].Groups = {};
        params.RawHubs.push(name);
        if (!params.RawGroups[name]) {
            params.RawGroups[name] = [];
        }
        if (extend) {
            if ((!self.Extend) || (!extend.append)) {
                self.Extend = extend;
            } else {
                if (!self.Extend) {
                    self.Extend = {};
                }
                if (extend.append) {
                    for (var extendIndex in extend) {
                        if (extendIndex !== "append") {
                            self.Extend = extend[extendIndex];
                        }
                    }
                } else {
                    self.Extend = extend;
                }
            }
            $.extend(params.Connection[name].client, self.Extend);
        } else if (self.Extend) {
            $.extend(params.Connection[name].client, self.Extend);
        }
        return true;
    };
    this.removeHub = function (name) {
        if (!self.Hubs[name]) {
            return false;
        }
        for (var groupIndex in self.Hubs[name].Groups) {
            self.Hubs[name].Groups[groupIndex].unsubscribe();
        }
        delete self.Hubs[name];
        delete params.RawGroups[name];
        params.RawHubs.splice(params.RawHubs.indexOf(name), 1);
        return true;
    };
    this.addGroupToHub = function (hubName, groupName) {
        if ((self.Hubs[hubName]) && (self.Hubs[hubName].Groups[groupName])) {
            return false;
        }
        self.Hubs[hubName].server.subscribe(groupName);
        self.Hubs[hubName].Groups[groupName] = {}; //Here we can hold group-related data
        if (params.RawGroups[hubName].indexOf(groupName) < 0) {
            params.RawGroups[hubName].push(groupName);
        }
        return true;
    };
    this.removeGroupFromHub = function (hubName, groupName) {
        if ((!self.Hubs[hubName]) || (!self.Hubs[hubName].Groups[groupName])) {
            return false;
        }
        self.Hubs[hubName].server.unsubscribe(groupName);
        delete self.Hubs[hubName].Groups[groupName];
        if (params.RawGroups[hubName].indexOf(groupName) >= 0) {
            params.RawGroups[hubName].splice(params.RawGroups[hubName].indexOf(groupName), 1);
        }
        return true;
    };
    for (var hubIndex in params.RawHubs) {
        self.addHub(params.RawHubs[hubIndex]);
    }
    params.Connection.hub.start().done(function () {
        for (var hubIndex in params.RawGroups) {
            for (var groupIndex in params.RawGroups[hubIndex]) {
                self.addGroupToHub(hubIndex, params.RawGroups[hubIndex][groupIndex]);
            }
        }
    });
}

我正在使用它,例如:

    function statusUpdate(status) {
        alert(status);
    }
    var signalRUpdater = new SignalRUpdater({
        RawHubs: ["statusUpdates"],
        Extend: {
            statusUpdate: statusUpdate
        }
    });

到目前为止,这么好。但是,我可能在同一个集线器中有几个组,并且statusUpdate我似乎不知道该组。我可以将它从服务器端作为参数发送到statusUpdate,但是我想知道这是否是一种矫枉过正的问题,是否可以开箱即用SignalR。

1 个答案:

答案 0 :(得分:1)

向客户端发送组消息时,服务器不会发送消息发送到的组的名称。服务器选择作为该组成员的客户端,并仅向其发送消息。如果您想了解SignalR正在使用的协议,您可以找到我之前写的描述here