如何使用使用WebSockets /插座IO / nowjs Backbone.js的 [英] How to use backbone.js with websockets/socket-io/nowjs

查看:102
本文介绍了如何使用使用WebSockets /插座IO / nowjs Backbone.js的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入Backbone.js的和正在寻找的进展有点慢。我的主要问题是要找出如何使用插座-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.

推荐答案

简单地覆盖 Backbone.sync ,以便它发送消息下来socket.io并告诉相关backbonejs车型在服务器上修改状态。

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.

因此​​,对于每一个客户,他们拥有一套M其中客户端在M全系车型的主,有一组S其中客户端在S中所有车型。

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车型在服务器上,只有一个客户端应该有并购一个特定的模式(或者你需要实现一个坚实的锁定/合并执行)。

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

每当服务器上的模型更新你根本推送到谁在S.该模型的任何客户端(推到谁拥有在并购这种模式如果模型是在并购了多个客户端的任何客户端)。

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

一个颇费思量的需要去成通常由控制器MVC一旦客户端POST / PUT处理控制/权限和所有权/删除一些数据。

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.

这篇关于如何使用使用WebSockets /插座IO / nowjs Backbone.js的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆