如何将backbone.js与websockets/socket-io/nowjs一起使用
我刚刚进入backbone.js,发现进展有点慢。我的主要问题是如何使用 socket-io 使客户端和服务器端模型保持同步(从技术上讲,我使用的是 now.js,但应该应用相同的原理)。
我认为最好的方法是覆盖同步方法,但一些简单的建议将非常受欢迎。
I am just getting into backbone.js and am finding progress a little slow. My main problem is working out how to keep my client and server side models in sync using socket-io (technically I am using now.js but the same principal should apply).
I think the best way is to override the sync method but some simple advice would be really welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需覆盖 Backbone.sync,以便它向 socket.io 发送消息并告诉服务器上的相关 backbonejs 模型更改状态。
该解决方案有趣的部分是建立主主关系。您需要确保对于任何客户端,他们只能“更新”他们拥有“所有权”的服务器上模型的状态,以避免黑客和服务器端状态损坏。
因此,对于每个客户端,它们有一个集合 M,其中该客户端是 M 中所有模型的主控,并且有一个集合 S,其中该客户端拥有 S 中所有模型的从属。
它只能在服务器上强制更新 M 中的模型,并且只有一个客户端应该在 M 中具有特定模型(或者您需要实现可靠的锁定/合并实现)。
每当服务器上的模型更新时,您只需将其推送到 S 中具有该模型的任何客户端(如果多个客户端的模型位于 M 中,则推送到 M 中具有该模型的任何客户端)。
一旦客户端 POST/PUT/DELETE 某些数据,通常由 MVC 控制器处理的控制/权限和所有权需要进行大量思考。
Simply overwrite
Backbone.sync
so that it sends messages down socket.io and tells the relevant backbonejs models on the server to alter state.The interesting part of this solution is setting up the master-master relationship. You need to insure that for any client they can only "update" the state of models on the server that they have "ownership" of to avoid hackers and server-side state corruption.
So for each client they have a set M where that client is the master of all models in M and has a set S where that client has slaves of all the models in S.
It can only force updating on the server of models in M and only one client should have a particular model in M (or you need to implement a solid locking / merging implementation).
Whenever a model on the server is updated you simply push out to any client who has that model in S. (and push to any client who has that model in M if the model is in M for multiple clients).
A lot of thought needs to go into control / permissions and ownership that is normally handled by the MVC controller once a client POST/PUT/DELETE some data.
查看backbone.iobind:https://github.com/noveogroup/backbone.iobind
为您覆盖
Backbone.sync
。Check out backbone.iobind: https://github.com/noveogroup/backbone.iobind
It overrides
Backbone.sync
for you.更好的方法是使用事件聚合器的事件驱动架构。关于这个主题的精彩读物是以下德里克·贝利 (Derick Bailey) 的文章 => 骨干应用程序与 WebSocket 解耦
将 高度解耦,可以更轻松地测试和更改 websockets 库,最重要的是,它不会与重写 Backbone 的内部结构相混淆,例如同步()
A much better approach is event-driven architecture using an event aggregator. Great read on the subject is the following Derick Bailey's article => Decoupling Backbone Apps From WebSockets
It keeps stuff highly decoupled, enables easier testing and changing websockets lib, and on top of it all, it doesn't mess up with overriding Backbone's internals like sync()
也许这个优秀的教程会对您有所帮助:
https:// blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node
Maybe this excellent tuto will help you:
https://blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node