SignalR仅在发送时接收消息

时间:2020-04-20 13:16:16

标签: signalr signalr-hub signalr.client signalr-backplane

我们在.Net Framework中拥有一个SignalR服务器,该服务器可同时为Windows和Web客户端提供服务。

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

问题适用于以下情况。

如果发件人是Windows应用程序,而收件人是Web应用程序,则除非Web App单击我们具有的“发送消息”按钮,否则Web App不会收到消息。

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

Web客户端代码:

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

谢谢, 密室

0 个答案:

没有答案