我们当前使用SignalR在我们的UI客户端上从后端接收实时消息。 UI客户端在线并连接到SignalR时会收到消息,而在断开连接时会丢失消息(例如:用户关闭页面,而SignalR断开客户端连接)。 但是,现在我们需要向用户显示所有消息,包括UI Client离线时SignalR发送的消息。 SignalR可以支持这种情况吗? 该要求类似于UI客户端消息的持久队列,但是我们使用SignalR向所有客户端广播消息。
答案 0 :(得分:1)
SignalR不支持这种情况,您需要自己完成。您需要存储消息并实现集线器方法,该方法将将待处理数据发送到连接的客户端。因此,您需要做的是:
readby
选项将数据保存在一些易失性存储中,这样您就可以查看已发送到客户端的数据并将其删除。代码示例,在客户端,连接并获取以前的数据:
/**
* Connect signalR and get previous data
*/
private async connectSignalR() {
await this.hubMessageConnection.start()
.then(() => {
// Register application
this.GetDataForThisClientAsync();
}).catch(() => {
this.onError.emit(WidgetStateEnum.connectError);
});
}
以及获取数据的中心方法:
public async Task<OperationResult> GetNotificationsAsync(Groups groups) { IList<MyData> data = await this.DataManager.GetDataForThisClientAsync(groups).ConfigureAwait(false); if (data.Count != 0) { // Send the notifications foreach (MyData data in datas) { await this.BroadcastDataToCallerAsync(data).ConfigureAwait(false); } } return OperationResult.Success(); }