【问题标题】:Syncing app state with clients using socketio使用 socketio 与客户端同步应用程序状态
【发布时间】:2019-03-03 01:53:06
【问题描述】:

我正在运行一个带有 SocketIO 的节点服务器,它保持定期更新的大对象(应用程序状态)。

所有客户端在连接到服务器后都会收到该对象,并应使用套接字实时更新它(只读)。

这是我考虑过的:

1: 更新后使用diff 向客户端发送更改增量 (需要处理交付的可靠性和丢失的更新)

2: 使用diffsync 包(但是它允许客户端将更改推送到服务器,但我需要更新是单向的,即服务器-->客户端)

我相信应该有一个现成的解决方案来解决这个问题,但我无法找到明确的答案。

【问题讨论】:

    标签: node.js websocket socket.io synchronization diff


    【解决方案1】:

    解决方案非常简单。您必须 modify the server 以便它只接受来自受信任客户端的更新。

    let Server = require('diffsync').Server;
    let receiveEdit  = Server.prototype.receiveEdit 
    Server.receiveEdit = function(connection, editMessage, sendToClient){
      if(checkIsTrustedClient(connection))
        receiveEdit.call(this, connection, editMessage, sendToClient)
    }
    

    but

        // TODO: implement backup workflow
        // has a low priority since `packets are not lost` - but don't quote me on that :P
        console.log('error', 'patch rejected!!', edit.serverVersion, '->', 
                clientDoc.shadow.serverVersion, ':',
                edit.localVersion, '->', clientDoc.shadow.localVersion);
    

    第二个选项是尝试根据jsondiffpatch寻找另一个解决方案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 1970-01-01
      • 2018-06-18
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 2011-05-27
      • 2022-07-13
      相关资源
      最近更新 更多