Javascript 信号器2.0未呼叫已连接

Javascript 信号器2.0未呼叫已连接,javascript,jquery,.net,signalr,Javascript,Jquery,.net,Signalr,我的集线器的这部分工作正常 这意味着配置(IAppBuilder应用程序)确实会被调用,但在此之后,我无法获取OnConnected、OnDisconnected、OnReconnected以返回到有人刚刚连接的客户端 我是否需要添加除app.mapsigner()之外的其他内容到配置(IAppBuilder应用程序) -----------------------我想分享我使用SignalR制作的一个插件------------------------------------------02-

我的集线器的这部分工作正常

这意味着配置(IAppBuilder应用程序)确实会被调用,但在此之后,我无法获取OnConnected、OnDisconnected、OnReconnected以返回到有人刚刚连接的客户端

我是否需要添加除
app.mapsigner()之外的其他内容
配置(IAppBuilder应用程序)

-----------------------我想分享我使用SignalR制作的一个插件------------------------------------------02-14-2014

插件

var signalRManager = function () {

    //$.connection.hub.logging = true
    var SiGNALR_CONNECTION_TIMEOUT = 5000,
        isGoogleBot = navigator.userAgent.toLowerCase().indexOf('googlebot') > 0;

    var events = {},
       eventTypes = {
           connected: 'connected',
           disconnected: 'disconnected',
           reconnected: 'reconnected',
           reconnecting: 'reconnecting'
       };

    var tryingToReconnect = false,
        reconnectINTERVAL;

    var subscribe = function (channel, fn) {

        if (channel instanceof Array) {

            for (var i = 0; i < channel.length; i += 1) {
                subscribe.call(this, channel[i], fn);
            };
        } else {

            if (!events[channel]) { events[channel] = [] };
            events[channel].push({ context: this, callback: fn });
        };
        return this;
    },
        publish = function (channel, eventArgs, fn) {

            if (!events[channel]) return signalRManager;
            channel = events[channel];
            var i = channel.length;
            while (i--) { channel[i].callback.call(channel[i].context, eventArgs, fn); };
            return signalRManager;
        },
        removeChannel = function (channel) {
            if (events[channel]) { delete events[channel]; };
            return signalRManager;
        },
        removeContext = function (channel) {

            if (!events[channel]) { return this; };

            channel = events[channel];
            var i = channel.length;
            while (i--) {

                if (channel[i].context === this) {
                    channel.splice(i, 1);
                    continue;
                };
            };

            return elevationEvents;
        },
        removeMe = function () {

            for (var ev in events) {
                var i = events[ev].length;
                while (i--) {
                    if (events[ev][i].context === this) { events[ev].splice(i, 1); };
                };
            };
            return this;
        },
        removeAll = function () {

            events = {};
            dbEvents = {};

            return signalRManager;
        };


    //....................Global SignalR Connections State...............................//
    if (!isGoogleBot) {
        setTimeout(function () {
            var cnnStateChanged = function (change) {

                if (change.newState === $.signalR.connectionState.reconnecting) {

                    tryingToReconnect = true;
                    publish(eventTypes.reconnecting, {});
                }
                else if (change.newState === $.signalR.connectionState.connected) {

                    clearInterval(reconnectINTERVAL);
                    tryingToReconnect = false;
                    publish(eventTypes.connected, {});
                }
                else if (change.newState === $.signalR.connectionState.disconnected) {

                    if (tryingToReconnect) {
                        //function to notify user.
                    };
                    reconnectINTERVAL = setInterval(function () {
                        $.connection.hub.start();
                    }, 5000); // Restart connection after 5 seconds

                    publish(eventTypes.disconnected, {});
                };
            };

            $.connection.hub.reconnected(function () {

                clearInterval(reconnectINTERVAL);
                tryingToReconnect = false;
                publish(eventTypes.reconnected, {});
            });

            $.connection.hub.stateChanged(cnnStateChanged);

            if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected) { $.connection.hub.start(); };

        }, SiGNALR_CONNECTION_TIMEOUT);
    };
    //....................Global SignalR Connections State end...........................//

    return {
        removeChannelSignalR: removeChannel,
        eventTypesSignalR: eventTypes,
        channelsSignalR: {},
        publishSignalR: publish,
        subscribeToSignalR: subscribe,
        removeMeSignalR: removeMe,
        removeContextSignalR: removeContext,
        removeAllSignalR: removeAll,

        installTo: function (obj) {
            obj.subscribeToSignalR = subscribe;
            obj.publishSignalR = publish;
        },
        unInstall: function (obj) {
            delete obj.subscribeToSignalR;
            delete obj.publishSignalR;
        }
    };
}();

我只是想猜测(因为您没有显示客户端代码),在调用start之前,您没有在hub上订阅客户端事件。您完全正确。谢谢,是的。。它的全部内容是在调用start方法之前附加客户机事件。谢谢@dflower
var signalRManager = function () {

    //$.connection.hub.logging = true
    var SiGNALR_CONNECTION_TIMEOUT = 5000,
        isGoogleBot = navigator.userAgent.toLowerCase().indexOf('googlebot') > 0;

    var events = {},
       eventTypes = {
           connected: 'connected',
           disconnected: 'disconnected',
           reconnected: 'reconnected',
           reconnecting: 'reconnecting'
       };

    var tryingToReconnect = false,
        reconnectINTERVAL;

    var subscribe = function (channel, fn) {

        if (channel instanceof Array) {

            for (var i = 0; i < channel.length; i += 1) {
                subscribe.call(this, channel[i], fn);
            };
        } else {

            if (!events[channel]) { events[channel] = [] };
            events[channel].push({ context: this, callback: fn });
        };
        return this;
    },
        publish = function (channel, eventArgs, fn) {

            if (!events[channel]) return signalRManager;
            channel = events[channel];
            var i = channel.length;
            while (i--) { channel[i].callback.call(channel[i].context, eventArgs, fn); };
            return signalRManager;
        },
        removeChannel = function (channel) {
            if (events[channel]) { delete events[channel]; };
            return signalRManager;
        },
        removeContext = function (channel) {

            if (!events[channel]) { return this; };

            channel = events[channel];
            var i = channel.length;
            while (i--) {

                if (channel[i].context === this) {
                    channel.splice(i, 1);
                    continue;
                };
            };

            return elevationEvents;
        },
        removeMe = function () {

            for (var ev in events) {
                var i = events[ev].length;
                while (i--) {
                    if (events[ev][i].context === this) { events[ev].splice(i, 1); };
                };
            };
            return this;
        },
        removeAll = function () {

            events = {};
            dbEvents = {};

            return signalRManager;
        };


    //....................Global SignalR Connections State...............................//
    if (!isGoogleBot) {
        setTimeout(function () {
            var cnnStateChanged = function (change) {

                if (change.newState === $.signalR.connectionState.reconnecting) {

                    tryingToReconnect = true;
                    publish(eventTypes.reconnecting, {});
                }
                else if (change.newState === $.signalR.connectionState.connected) {

                    clearInterval(reconnectINTERVAL);
                    tryingToReconnect = false;
                    publish(eventTypes.connected, {});
                }
                else if (change.newState === $.signalR.connectionState.disconnected) {

                    if (tryingToReconnect) {
                        //function to notify user.
                    };
                    reconnectINTERVAL = setInterval(function () {
                        $.connection.hub.start();
                    }, 5000); // Restart connection after 5 seconds

                    publish(eventTypes.disconnected, {});
                };
            };

            $.connection.hub.reconnected(function () {

                clearInterval(reconnectINTERVAL);
                tryingToReconnect = false;
                publish(eventTypes.reconnected, {});
            });

            $.connection.hub.stateChanged(cnnStateChanged);

            if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected) { $.connection.hub.start(); };

        }, SiGNALR_CONNECTION_TIMEOUT);
    };
    //....................Global SignalR Connections State end...........................//

    return {
        removeChannelSignalR: removeChannel,
        eventTypesSignalR: eventTypes,
        channelsSignalR: {},
        publishSignalR: publish,
        subscribeToSignalR: subscribe,
        removeMeSignalR: removeMe,
        removeContextSignalR: removeContext,
        removeAllSignalR: removeAll,

        installTo: function (obj) {
            obj.subscribeToSignalR = subscribe;
            obj.publishSignalR = publish;
        },
        unInstall: function (obj) {
            delete obj.subscribeToSignalR;
            delete obj.publishSignalR;
        }
    };
}();
function AlumChat(container, currentUser) {

    signalRManager.installTo(this);

    this.subscribeToSignalR.call(this, signalRManager.eventTypesSignalR.connected, function () {

        $.connection.hub.qs = ["acroomid=8"/*beta testers*/, '&uid=', userid, '&ispro=', isPro, '&prevRid=8'].join('');
        $.connection.hub.proxies.alumchathub.server.getChatRooms();
    });

    //.....................Server Calls Client................................/
    $.connection.hub.proxies.alumchathub.client.receiveMsg = function (alumCloudMsg) {

        for (var i = 0; i < alumCloudMsg.msgs.length; i++) {
            that.chats.appendChild(AlumCloud.makeMessage({ message: alumCloudMsg.msgs[i], cnnid: alumCloudMsg.cnnid, parentContainer: that.chats, excludeDeleteButton: true, excludeCheckBox: true }).setStyles({ 'paddingLeft': '5px' }));
        };
    };
    $.connection.hub.proxies.alumchathub.client.receiveRooms = function (dbRooms) {

        for (var i = 0; i < dbRooms.length; i += 1) { selRooms.appendChild(new Option(dbRooms[i].Name, dbRooms[i].ID)); };
        head.addChilds([selRooms, btnLeave]);
    };
    $.connection.hub.proxies.alumchathub.client.gone = function (newAttendee) {

        //console.log('gone : ' + attendee.cnnid);
        if (rooms[selRooms.value].chatters.querySelector('.' + newAttendee.Alias)) {
            rooms[selRooms.value].chatters.querySelector('.' + newAttendee.Alias).removeMe();
        };

    };
    $.connection.hub.proxies.alumchathub.client.rejoined = function (newAttendee) {

        //console.log('Re joined .rejoined: ' + newAttendee.cnnid);

        if (!rooms[selRooms.value].chatters.querySelector('.' + newAttendee.Alias)) {

            rooms[selRooms.value].chatters.appendChild(
                docCreateAttrs('img', {
                    src: newAttendee.profile.Avatar,
                    'data-id': newAttendee.profile.ID,
                    'data-uname': newAttendee.Alias,
                    'data-cnnid': newAttendee.cnnid,
                    title: (newAttendee.profile.IsPro ? newAttendee.profile.CompanyName : ([newAttendee.profile.FName, ' ', newAttendee.profile.LName, (newAttendee.profile.CompanyName ? (' @' + newAttendee.profile.CompanyName) : '')].join(''))),
                    'class': 'rc siteAvatar ' + newAttendee.Alias/*for quering*/,
                    onclick: AlumCloud.miniHandler,
                    profile: newAttendee.profile
                }));


            if (newAttendee.profile.UserID === userid/*global userid*/) {

                meAttendee = newAttendee;
            };

            //let the new attendee know about you
            if (newAttendee.profile.UserID !== userid/*global userid*/) {

                meAttendee.newbieid = newAttendee.cnnid;
                $.connection.hub.proxies.alumchathub.server.addMeNewbie(meAttendee);
            };
        };
    };
    $.connection.hub.proxies.alumchathub.client.joinedChat = function (newAttendee) {
        var joinedTO;
        if (!rooms[selRooms.value].chatters.querySelector('.' + newAttendee.Alias)) {

            rooms[selRooms.value].chatters.appendChild(
                docCreateAttrs('img', {
                    src: newAttendee.profile.Avatar,
                    'data-id': newAttendee.profile.ID,
                    'data-cnnid': newAttendee.cnnid,
                    'data-uname': newAttendee.Alias,
                    title: (newAttendee.profile.IsPro ? newAttendee.profile.CompanyName : ([newAttendee.profile.FName, ' ', newAttendee.profile.LName, (newAttendee.profile.CompanyName ? (' @' + newAttendee.profile.CompanyName) : '')].join(''))),
                    'class': 'rc siteAvatar ' + newAttendee.Alias/*for quering*/,
                    onclick: AlumCloud.miniHandler,
                    profile: newAttendee.profile
                }));

            if (newAttendee.profile.UserID === userid/*global userid*/) { meAttendee = newAttendee; };

            //let the new attendee know about you
            if (newAttendee.profile.UserID !== userid/*global userid*/) {
                meAttendee.newbieid = newAttendee.cnnid;
                $.connection.hub.proxies.alumchathub.server.addMeNewbie(meAttendee);
            };

            //notification information
            var xy = head.getMyGlobalPosition(), jName;
            if (~~newAttendee.profile.IsPro > 0) {
                jName = newAttendee.profile.CompanyName + ' Joined Room'
            } else {
                jName = newAttendee.profile.FName + ' ' + newAttendee.profile.LName + ' Joined Room'
            };

            document.body.appendChild(notifyBubble.addChild(docCreateAttrs('p', { innerHTML: jName })));
            notifyBubble.setStyles({ left: (xy.left + 20) + 'px', top: ((xy.top - 45) - parseInt(notifyBubble.getHeight()) / 2) + 'px' });

            clearTimeout(joinedTO);
            joinedTO = setTimeout(function () {
                notifyBubble.removeMe();
                notifyBubble.innerHTML = '';
            }, 5000);
            //notification information end
        };
    };
    $.connection.hub.proxies.alumchathub.client.addCurrAttendee = function (newAttendee) {

        if (!rooms[selRooms.value].chatters.querySelector('.' + newAttendee.Alias)) {
            rooms[selRooms.value].chatters.appendChild(
                docCreateAttrs('img', {
                    src: newAttendee.profile.Avatar,
                    'data-id': newAttendee.profile.ID,
                    'data-uname': newAttendee.Alias,
                    'data-cnnid': newAttendee.cnnid,
                    title: (newAttendee.profile.IsPro ? newAttendee.profile.CompanyName : ([newAttendee.profile.FName, ' ', newAttendee.profile.LName, (newAttendee.profile.CompanyName ? (' @' + newAttendee.profile.CompanyName) : '')].join(''))),
                    'class': 'rc siteAvatar ' + newAttendee.Alias/*for quering*/,
                    onclick: AlumCloud.miniHandler,
                    profile: newAttendee.profile
                }));
        };
    };
};