在我的网页应用程序中,我希望在上电时从服务器端将所有数据加载到客户端。之后,我希望所有通信都通过Signalr进行管理——这意味着每次更新时,服务器将向所有客户端发送通知并要求它们请求更新的数据。
然而,当SingalR连接中断并重新连接时,我不知道该怎么做。我不想再重新加载所有数据。我想做的是在服务器端为每个断开连接的客户端实现一些通知管理,并在SignalR连接恢复时,将错过的所有通知推送给具体的客户端。
我们在客户端使用单例监听器代替短暂的控制器作为SignalR侦听器,以便防止在每个视图更改时进行GET请求,并使应用程序更快速、更用户友好。因此,即使对于最终用户当前不相关的当前视图,后台中的新通知也会得到处理和处理,如下所示:
然而,当SingalR连接中断并重新连接时,我不知道该怎么做。我不想再重新加载所有数据。我想做的是在服务器端为每个断开连接的客户端实现一些通知管理,并在SignalR连接恢复时,将错过的所有通知推送给具体的客户端。
我们在客户端使用单例监听器代替短暂的控制器作为SignalR侦听器,以便防止在每个视图更改时进行GET请求,并使应用程序更快速、更用户友好。因此,即使对于最终用户当前不相关的当前视图,后台中的新通知也会得到处理和处理,如下所示:
// This service is initialized once only
public class Service1 {
static inject = ['$rootScope']
array : Item[];
// This is a singleton!
public constructor ($rootScope){
// Get all items from the server
GetAllItemsFromServer();
// Listener for signalR updates
var listener = $rootScope.$on("ItemsNotificationFromServer", UpdateItems);
$rootScope.$on('destroy', {
// Stop the listener
listener();
})
}
// Getting all the items from the server on each controller creation
GetAllItemsFromServer(){
// Getting the items
}
// Handle the notification from the server
public UpdateItems(event, result) : void
//..
}
}
目前的情况是,例如当终端用户刷新浏览器(F5)时,我无法知道此客户端在连接问题期间错过了哪些SignalR通知,因此我必须重新从服务器加载所有数据(很糟糕)。
为了防止这种情况发生,我考虑实现类似以下的内容 -
namespace MapUsersSample
{
public class UserContext : DbContext
{
// All those are cleaned when server is powered up
public DbSet<Connection> Connections { get; set; }
public DbSet<Notification> Notifications {get; set;}
}
public class Connection
{
[Key]
[DatabaseGenerationOptions.None]
public string ConnectionID { get; set; }
public bool Connected { get; set; }
// I fill this when disconnected
public List<Notification> MissedNotifications {get; set;}
public Connection(string id)
{
this.ConnectionID = id;
this.Connected = true;
this.MissedNotifications = new List<Notification>();
}
}
public abstract class Notification()
{
public int Id {get; set;}
public DateTime CreationTime {get; set;}
}
.. // Many notifications implement this
}
public class MyHub : Hub
{
private readonly DbContext _db;
public class MyHub(DbContext db)
{
this._db = db;
}
// Adding a new connection or updating status to true
public override Task OnConnected()
{
var connection = GetConnection(Context.ConnectionId);
if (connection == null)
_db.Connections.Add(new Connection(Context.ConnectionId));
else
connection.Connected = true;
return base.OnConnected()
}
// Changing connection status to false
public override Task OnDisconnected()
{
var connection = GetConnection(Context.ConnectionId);
if (connection == null)
{
Log("Disconnect error: failed to find a connection with id : " + Context.ConnectionId);
return;
}
else {
connection.Connected = false;
}
return base.OnDisconnected();
}
public override Task OnReconnected()
{
var connection = GetConnection(Context.ConnectionId);
if (connection == null)
{
Log("Reconnect error - failed to find a connection with id : " + Context.ConnectionId);
return;
}
else {
connection.Connected = true;
}
// On reconnect, trying to send to the client all the notifications that he has missed
foreach (var notification in connection.MissedNotifications){
Clients.Client(connection.ConnectionID).handleNotification(notification);
}
return base.OnReconnected();
}
// This method is called from clients that receive a notification
public clientNotified(int connectionId, int notificationId)
{
// Getting the connection
var connection = GetConnection(connectionId);
if (connection == null){
Log("clientNotified error - failed to find a connection with id : " + Context.ConnectionId);
return;
}
// Getting the notification that the client was notified about
var notificationToRemove = _dbConnection.Notifications.FirstOrDefault(n => n.Id == notificationId);
if (notificationToRemove == null)
{
Log("clientNotified error - failed to find notification with id : " + notificationId);
return;
}
// Removing from the missed notifications
connection.MissedNotifications.Remove(notificationToRemove);
}
private Connection GetConnection(int connectionId)
{
return _db.Connections.find(connectionId);
}
}
// Notifications outside of the hub
public class Broadcaster
{
DbContext _db;
public Broadcaster(DbContext db)
{
_hubContext = GlobalHost.ConnectionManager.GetHubContext<MoveShapeHub>();
_dbConnection = db;
}
public void NotifyClients(Notification notification)
{
var openConnections = _db.Connections.Where(x => x.Connected);
var closedConnections = _db.Connections.Where(x => !x.Connected);
// Adding all notifications to be sent when those connections are back
foreach (var connection in closedConnections){
connection.MissedNotifications.add(notification);
}
// Notifying all open connections
foreach (var connection in openConnections){
_hubContext.Clients.Client(connection.ConnectionID).handleNotification(notification);
}
}
}
client side java script:
handleNotification(notification){
hubProxy.Server.clientNotified(hub.connection.id, notification.Id)
// Keep handling the notification here..
}
我还没有测试过这种方法,但在向我的团队提出这个想法之前,这种方法是否很流行呢?我没有看到有人采取这种方法,我想知道为什么?这里是否存在任何风险?