SignalR 2.0不调用OnConnected

时间:2013-11-06 16:49:53

标签: javascript jquery .net signalr

我的中心部分工作正常。

意味着配置(IAppBuilder应用程序)确实被调用,但之后我无法获得OnConnected,OnDisconnected,OnReconnected以返回我刚刚连接的客户端。

我是否需要将app.MapSignalR();以外的内容添加到Configuration(IAppBuilder app)

[assembly: OwinStartup(typeof(AlumCloud.hubs.CollaboratorHub))]
namespace AlumCloud.hubs
{
    public class CollaboratorHub : Hub
    {
        // I need to fix the isGeneric params and remove the fixed false value
        public void Configuration(IAppBuilder app)
        {
            // Any connection or hub wire up and configuration should go here
            app.MapSignalR();
        }

-----------------------我想分享一个使用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;
        }
    };
}();

订阅插件。我故意遗漏了以下大部分代码

- 请注意下面函数中的signalRManager.installTo(this); -

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
                }));
        };
    };
};

0 个答案:

没有答案