首页 > 解决方案 > SignalR 一个连接多个集线器,多个处理程序

问题描述

理想情况下,我希望与多个集线器建立一个连接,但在连接开始后注册客户端处理程序。

什么有效:

        //multiple connections method = works
        var _page_connection = $.hubConnection();
        var _page_connection2 = $.hubConnection();
        function connectionOne() {
            var proxy1 = _page_connection.createHubProxy('hub_Echo');
            if (_page_connection && _page_connection.state === $.signalR.connectionState.disconnected) {
                proxy1.on('echo', function (msg) { console.log('Echo1: ' + msg); });
                _page_connection.start()
                    .done(function () { console.log('Now connected, connection ID=' + _page_connection.id); })
                    .fail(function () { console.log('Could not connect'); });
            } else {
                console.log('Already connected, connection ID=' + _page_connection.id);
                proxy1.on('echo', function (msg) { console.log('Echo1: ' + msg); });
                _page_connection.start()
                    .done(function () { console.log('Connected again, connection ID=' + _page_connection.id); })
                    .fail(function () { console.log('Could not connect'); });
            }
            $('#btn_echosend').on('click', function () {
                proxy1.invoke('echo', 'marco');
            });
        }

        function connectionTwo() {
            var proxy2 = _page_connection2.createHubProxy('hub_Echo2');
            if (_page_connection2 && _page_connection2.state === $.signalR.connectionState.disconnected) {
                proxy2.on('echo2', function (msg) { console.log('Echo2: ' + msg); });
                _page_connection2.start()
                    .done(function () { console.log('Now connected, connection ID=' + _page_connection2.id); })
                    .fail(function () { console.log('Could not connect'); });
            } else {
                console.log('Already connected, connection ID=' + _page_connection.id);
                proxy2.on('echo2', function (msg) { console.log('Echo2: ' + msg); });
                _page_connection2.start()
                    .done(function () { console.log('Connected again, connection ID=' + _page_connection2.id); })
                    .fail(function () { console.log('Could not connect'); });
            }
            $('#btn_echosend2').on('click', function () {
                proxy2.invoke('echo2', 'polo');
            });
        }

什么不起作用:

        function SignalRClient(methods) {
            this._handlers = {};
            methods.forEach(this.registerHandler.bind(this));
        }

        SignalRClient.prototype.invokeHandler = function (name) {
            var handler = this._handlers[name];
            if (handler) {
                var handlerArgs = Array.prototype.slice.call(arguments, 1);
                handler.apply(this, handlerArgs);
            }
        };

        SignalRClient.prototype.registerHandler = function (name) {
            var getter = this.invokeHandler.bind(this, name);
            Object.defineProperty(this, name, {
                enumerable: true,
                get: function () {
                    return getter;
                },
                set: function (value) {
                    this._handlers[name] = value;
                }.bind(this)
            });
        };
        function connectionTwo() {             
            $.connection.hub_Echo2.client = new SignalRClient(['echo', 'echo2']);                        
            $.connection.hub_Echo2.connection.start().done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })                    
            $.connection.hub_Echo2.client.echo = function (data) { console.log('Echo2: ' + data); };
            $('#btn_echosend2').on('click', function () {
                $.connection.hub_Echo2.server.echo('Polo');
            });
        }
        function connectionOne() {                        
            $.connection.hub_Echo.client = new SignalRClient(['echo', 'echo2']);
            $.connection.hub_Echo.connection.start().done(function () { console.log('Now connected, connection ID=' + $.connection.hub.id); })                    
            $.connection.hub_Echo.client.echo = function (data) { console.log('Echo: ' + data); };
            $('#btn_echosend').on('click', function () {
                $.connection.hub_Echo.server.echo('Marco');
            });
        }

标签: javascriptc#signalr

解决方案


推荐阅读