Signalr doesn't call client side functions

前端 未结 1 708
难免孤独
难免孤独 2020-12-31 08:05

I\'m using the VS2012 \"Fall\" Update with the Signalr 1.0.0 package. Calling server side functions works fine. However client functions are not called. Nothing seems to hap

1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 08:45

    So your issue lies in the fact that you're starting your connection before you have any client side methods registered with your hub proxy.

    There are two ways to solve this.

    1. In your init prior to $.connection.hub.start call your _subscribe method.
    2. Ensure that you have at least 1 client side method defined prior to start (required to be "subscribed" to incoming messages). Use .on to subscribe hub methods after start.

    So here's the two approaches in code.

    1:

    var rtcom = new RealtimeConnection();
    rtcom.init({debug: true});
    
    RealtimeConnection: function () {    
        var
            _rtHub = $.connection.realtimeConnectionHub,
            _initialized = false,                
            _init = function (options, cb) {
                options = options || {};
    
                $.connection.hub.logging = options.debug;
    
                _rtHub.client.onBroadcastMessage = function() {
                    alert("message");
                };
    
                $.connection.hub.start().done(function () {
                    _initialized = true;
    
                    if (cb) { cb(); }
                });
            },
    
            _broadcast = function (message) {
                if (_initialized) {
                    _rtHub.server.broadcastMessage("testuser", message);
                }
            },
    
        return {
            initialized: _initialized,
            broadcast: _broadcast,
            init: _init
        };
    }
    
    $("#container").on("click", "button.chat-btnSendMessage", function () {
        rtcom.broadcast($(this).parent().find("input").val());
    });
    

    2:

    var rtcom = new RealtimeConnection();
    rtcom.init({debug: true}, function () {
        rtcom.subscribe(function (sender, message) {
            if (message) {
                alert("message");
            }
        });
    });
    
    RealtimeConnection: function () {    
        var
            _rtHub = $.connection.realtimeConnectionHub,
            _initialized = false,                
            _init = function (options, cb) {
                options = options || {};
    
                // Need to have at least 1 function registered to be subscribed to hub
                _rtHub.client.foo = function() {};
    
                $.connection.hub.logging = options.debug;
                $.connection.hub.start().done(function () {
                    _initialized = true;
    
                    if (cb) { cb(); }
                });
            },
    
            _broadcast = function (message) {
                if (_initialized) {
                    _rtHub.server.broadcastMessage("testuser", message);
                }
            },
    
            _subscribe = function (cb) {
                if (_initialized) {
                    _rtHub.on("onBroadcastMessage", cb);
                }
            };
    
        return {
            initialized: _initialized,
            broadcast: _broadcast,
            subscribe: _subscribe,
            init: _init
        };
    }
    
    $("#container").on("click", "button.chat-btnSendMessage", function () {
        rtcom.broadcast($(this).parent().find("input").val());
    });
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题