Signalr不调用客户端功能(Signalr doesn't call client si

2019-07-20 12:35发布

我使用VS2012“秋天”更新与Signalr 1.0.0包。 调用服务器端功能工作正常。 但是客户端功能都没有叫。 似乎没有任何时候发生onBroadcastMessage()见下文)被调用。

问题:

  • 我在想什么?
  • 有没有一种方法来调试客户端调用?

谢谢!

服务器代码:

using Microsoft.AspNet.SignalR;

namespace KPMain
{
    public class RealtimeConnectionHub : Hub
    {
        public void BroadcastMessage(string name, string message) {
            Clients.All.onBroadcastMessage(name, message);
        }
    }
}

路线报名:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes) {
        HubConfiguration hubConfig = new HubConfiguration();
#if DEBUG
        hubConfig.EnableDetailedErrors = true;
#endif
        routes.MapHubs(hubConfig);

        ...
    }
}

客户机代码(简化):

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

            $.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.client.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());
});

登录(使用Chrome):

[16:58:01 GMT+0100 (W. Europe Standard Time)] SignalR: Negotiating with '/signalr/negotiate'. jquery.signalR-1.0.0.js:54
[16:58:02 GMT+0100 (W. Europe Standard Time)] SignalR: Attempting to connect to SSE endpoint 'http://w2008r2portal/signalr/connect?transport=serverSentEvents&connectionT…EgDDEU0WLhHVzI7ZUnA0impltR7HYNTGHUJJYAXew1Leg2&connectionData=%5B%5D&tid=0' jquery.signalR-1.0.0.js:54
[16:58:03 GMT+0100 (W. Europe Standard Time)] SignalR: EventSource connected jquery.signalR-1.0.0.js:54
[16:58:03 GMT+0100 (W. Europe Standard Time)] SignalR: Now monitoring keep alive with a warning timeout of 13333.333333333332 and a connection lost timeout of 20000 


Request URL:http://xxx/signalr/send?transport=serverSentEvents&connectionToken=lnTczBgQB0dj...
Request Method:POST
Status Code:200 OK

Request Headers
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Authorization:Negotiate YIIFXQYGKwYBBQUCoIIFUTCCBU2...
Connection:keep-alive
Content-Length:148
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:w2008r2portal
Origin:http://w2008r2portal
Referer:http://w2008r2portal/?tab=Start
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
X-Requested-With:XMLHttpRequest

Query String Parameters
transport:serverSentEvents
connectionToken:lnTczB...

Form Data
data:{"H":"realtimeconnectionhub","M":"BroadcastMessage","A":["testuser","test"],"I":0}

Response Headers
Cache-Control:no-cache
Content-Type:application/json; charset=UTF-8
Date:Mon, 25 Feb 2013 15:58:04 GMT
Expires:-1
Persistent-Auth:false
Pragma:no-cache
Server:Microsoft-IIS/7.5
Transfer-Encoding:chunked
WWW-Authenticate:Negotiate oYGyMIGvoAMKAQChCwY...
X-AspNet-Version:4.0.30319
X-Content-Type-Options:nosniff
X-Powered-By:ASP.NET

Answer 1:

所以,你的问题在于,你开始你的连接你与你的集线器代理注册的任何客户端方法之前的事实。

有两种方法来解决这个问题。

  1. 在你的初始化之前$ .connection.hub.start打电话给你_subscribe方法。
  2. 确保你必须开始之前定义的至少1客户端的方法(必须是“订阅”传入消息)。 。对使用后开始认购轮毂的方法。

因此,这里的代码中的两种方法。

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

希望这可以帮助!



文章来源: Signalr doesn't call client side functions