将更改同步到文本字段
我正在 Flash 上尝试 P2P,遇到了一个小障碍,我想在继续之前澄清一下。技术本身(Flash)对于这个问题并不重要,因为我认为这个问题也发生在其他语言中。
我正在尝试创建一个可以由多人“实时”编辑的文档。就像 Google 文档一样。但我想知道,您建议如何同步每个人的文本?我的意思是,每次有人进行更改时,我是否应该向所有人发送文本字段中的所有文本消息?这看起来效率很低。
我认为必须有一种我可以学习和实现的设计模式,但我不知道从哪里开始。
最佳情况下,应用程序应仅向连接的客户端发送文档发生的更改,并具有某种缓冲区或纠错功能,可用于检索可能错过的早期更改。是否有任何既定的设计模式可以处理此类问题?
谢谢, 桑德罗
I'm experimenting with P2P on Flash, and I've come across a little hurdle that I'd like to clarify before moving forward. The technology itself (Flash) doesn't matter for this problem, as I think this problem occurs in other languages.
I'm trying to create a document that can be edited "live" by multiple people. Just like Google Docs pretty much. But I'm wondering, how would you suggest synchronizing everyone's text? I mean, should I message everyone with all the text in the text field every time someone makes a change? That seems very inefficient.
I'm thinking there has to be a design pattern that I can learn and implement, but I'm not sure where to start.
Optimally, the application should send the connected clients only the changes that have occurred to the document, and have some sort of buffer or error correction that can be used for retrieving earlier changes that may have been missed. Is there any established design pattern that deals with this type of issue?
Thanks,
Sandro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的“最佳”解决方案实际上是您应该寻求的解决方案。
每个文本字段都有一个模型,该模型有一个历史记录(FILO 存储最后一个值,比如说 10 个值)。
每次编辑该文本字段时,您都会将整个文本推送到模型中,并将增量发送到其他连接的客户端。
当其他客户端接收数据时,他们只需从模型中选择最后一个值并将其合并到接收到的数据中。
您可以通过在中间放置一个空闲计时器来完善该机制:当用户在文本字段中键入内容时,您将该模型标记为“toBeSentThroughTheNet”,然后启动一个计时器。当计时器“滴答作响”(TimerEvent.TIMER)时,您将其停止,收集标记的数据并将其发送到其他客户端。只需记住每次用户实际打字时重置计时器(简化可以是 keydown = 重置,keyup = 开始)。
还有一个优化可以发送打包在压缩字节数组中的数据,但这需要您编写自己的协议,并且可能不是一个简单快捷的路径:)
I think your "Optimally" solution is actually the one you should go for.
each textfield has a model, the model has a history (a FILO storing last, let's say, 10 values).
every time you edit that textfield you push the whole text into the model and send the delta to other connected clients.
as other clients receive the data they just pick the last value from the model and merge it to the received data.
you can refine the mechanism by putting an idle timer in the middle: as a user types something in the textfield you flag that model as "toBeSentThroughTheNet" and you start a timer. as the timer "ticks" (TimerEvent.TIMER) you stop it, collect the flagged data and send it to other clients. just remember to reset the timer everytime the user is actually typing (a semplification coul be keydown = reset, keyup = start).
one more optimization could be send the data packed in a compressed bytearray, but this requires you write your own protocol and may be not so an easy and quick path :)
如果要求是每个人都可以同时编辑文档,并且更改应该传播给每个人并且不应该丢失任何更改,那么这是一个不小的问题。目前有几种不同的方法,但一种非常强大的方法是操作转型。这与 Google 文档用于协作编辑的算法相同。
理解和应用运营转型以及随之而来的黑客新闻讨论可能是其他不错的起点。
Wave Protocol 已作为开源版本发布,因此您可以看看它是如何实现的。
当然,您可以放弃棘手的同步,只允许人们轮流,一次只有一个人可以编辑文档,而这个人只需将更改推送到组的其余成员。
If the requirement is that everyone can edit the document at the same time and the changes should be propagated to everyone and no changes should be lost, then it is a non-trivial problem. There are few different approaches out there, but one that is quite robust is Operational Transformation. This is the same algorithm that Google Docs uses for collaborative editing.
Understanding and Applying Operational Transformation and the attendant hacker news discussion are probably other good places to start.
The Wave Protocol was released as open source so you can take a look on how it is implemented.
You could of course forgo the tricky synchronization and just allow people to take turns and only one person can edit the document at a time and this person just pushes the changes to the remainder of the group.