signalr - 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;
};
谢谢, 瓦萨尔
解决方案
推荐阅读
- typescript - Visual Studio Code 自动完成显示了很多选项
- python - 为什么 Tensorflow 转置在某种情况下会失败?
- react-native - 是否可以规模化
随着缩放的增加和减少? - python - 类型提示中的可选联合
- java - 在Java中使用正则表达式查找非数字的索引
- docker - 以桥接模式部署 Mesos docker 容器,但端口固定且超出预定义范围
- perl - Perl - tan 的未定义子例程
- javascript - 将附加参数传递给事件回调函数
- android - 如何在恒定间隔的约束布局中居中两个文本视图?
- python - 我可以从 Excel 文件中修改特定工作表并写回相同的工作表而不使用 Pandas 修改其他工作表吗?打开pyxl