Javascript 如何在 websockets/socket-io/nowjs 中使用backbone.js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6657968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:39:50  来源:igfitidea点击:

How to use backbone.js with websockets/socket-io/nowjs

javascriptnode.jsbackbone.jssocket.ionowjs-sockets

提问by henry.oswald

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).

我刚刚进入backbone.js,发现进展有点慢。我的主要问题是如何使用 socket-io 使我的客户端和服务器端模型保持同步(从技术上讲,我使用的是 now.js,但应该应用相同的主体)。

I think the best way is to override the sync method but some simple advice would be reallywelcome.

我认为最好的方法是覆盖同步方法,但确实欢迎一些简单的建议。

回答by Raynos

Simply overwrite Backbone.syncso that it sends messages down socket.io and tells the relevant backbonejs models on the server to alter state.

简单地覆盖,Backbone.sync以便它向 socket.io 发送消息并告诉服务器上的相关主干模型改变状态。

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.

因此,对于每个客户端,他们有一个集合 M,其中该客户端是 M 中所有模型的主节点,并且有一个集合 S,其中该客户端具有 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).

它只能强制更新 M 中模型的服务器,并且只有一个客户端应该具有 M 中的特定模型(或者您需要实现可靠的锁定/合并实现)。

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).

每当服务器上的模型更新时,您只需推送到在 S 中具有该模型的任何客户端(如果模型在 M 中用于多个客户端,则推送到在 M 中具有该模型的任何客户端)。

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.

一旦客户端 POST/PUT/DELETE 一些数据,就需要考虑通常由 MVC 控制器处理的控制/权限和所有权。

回答by Evan Moran

Check out backbone.iobind: https://github.com/noveogroup/backbone.iobind

查看backbone.iobind:https: //github.com/noveogroup/backbone.iobind

It overrides Backbone.syncfor you.

Backbone.sync为您覆盖。

回答by zappan

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

更好的方法是使用事件聚合器的事件驱动架构。关于这个主题的好读物是以下 Derick Bailey 的文章 =>从 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()

它使东西高度解耦,使测试和更改 websockets 库更容易,最重要的是,它不会与像 sync() 一样覆盖 Backbone 的内部结构混乱

回答by Samber Valley

Maybe this excellent tuto will help you: https://blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node

也许这个优秀的教程会帮助你:https://blog.andyet.com/2011/02/15/re-using-backbonejs-models-on-the-server-with-node