首页 > 解决方案 > SignalR 仅在发送时接收消息

问题描述

我们在 .Net Framework 中有一个 SignalR 服务器,它服务于 Windows 和 Web 客户端。

对于 Web 客户端,我们使用为 SignalR 提供的 Javascript 库,并将它用于我们的 Angular 应用程序。

问题适用于以下场景。

如果发送方是 Windows 应用程序而接收方是 Web 应用程序,则 Web 应用程序不会收到消息,除非 Web 应用程序单击我们拥有的发送消息按钮。

与 web 到 windows 相同的场景。如果 Web 应用正在发送消息,则只有当 windows 应用再次发送消息时才会收到消息。

两者都使用同一个集线器,加入同一个组。

在检查时,我发现消息确实会发送到服务器,但是只有在调用发送消息时才会触发接收消息。

Web 到 Web 和 Windows 到 Windows 应用程序运行良好,没有任何问题。

如果需要更多信息,请告诉我。

集线器连接的 Windows 代码:

_hubConnection = new HubConnection(signalRHubAddress);

                        //We Add SAML Token into the header as hub is secured by SAML.

                        _hubConnection.Credentials = CredentialCache.DefaultCredentials;
                        _hubConnection.Closed += OnDisconnected;
                        _hubConnection.Reconnecting += OnReconnecting;
                        _hubConnection.Error += OnError;
                        _hubConnection.StateChanged += OnStateChanged;

                        _hubProxy = _hubConnection.CreateHubProxy(applicationHub);
                        _hubProxy["environmentName"] = "Environment";

                        _hubConnection.Start();

                        _hubProxy.On<string>("JoinGroupMessage", msg => ProcessJoinGroupMessage(msg,false));
                        _hubProxy.On<string>("ReJoinGroupMessage", msg => ProcessJoinGroupMessage(msg,true));
                        _hubProxy.On<string>("LeaveGroupMessage", msg => ProcessLeaveGroupMessage(msg));
                        _hubProxy.On<string>("ReceiveMessage", msg => ProcessReceiveMessage(msg));

网络客户端代码:

        $.ajax({
            url: hubParam.homeUrl,
            type: 'Post',
            dataType: "json",
            contentType: "application/x-www-form-urlencoded;charset=utf-8",
            data: {
              SAMLToken: token
            },
            success: function (Id) {
              myHub.assertionId = Id;
              myHub.connection = getConnection(hubParam.hubUrl);
              myHub.hubProxy = getHubProxy(myHub.connection, hubParam.hubName);

              myHub.hubProxy.on(clientFuncName, function (msg) {
               callbackFunc(msg);
              });

              myHub.connection.start().done(function () {
                  console.log(resources.connectionStarted);
                })
                .fail(function (err) {

                  console.log(resources.connectionFailed + err.toString());
                });       
            }

         getConnection = function(hubUrl) {
            if (myHub.connection == null || myHub.connection === 'undefined') {
                   myHub.connection = $.hubConnection(hubUrl);
                   myHub.connection.qs = {
                                   'version': myHub.version
                                         };
              }
            else if(myHub.connection.state === 4) {
                   myHub.connection.start();
              }
      return myHub.connection;
    };

getHubProxy = function (hubConn, hubName) {
  if (myHub.hubProxy == null || myHub.hubProxy === 'undefined') {
    myHub.hubProxy = hubConn.createHubProxy(hubName);
  }
  return myHub.hubProxy;
};

谢谢, 瓦萨尔

标签: signalrsignalr-hubsignalr.clientsignalr-backplane

解决方案


推荐阅读