节点共享服务器与客户端之间的数据
有没有一种方法使用node&socketIo / express以某种方式将一个对象variables绑定到客户端,所以它的行为就像被动meteor一样,并保持与meteor一样的同步方式?
换句话说,例如,我在我的node.js脚本中有一个JSON对象variables:
var marksObject = { firstValue : 'this is the first value' , secondValue : 'this is the second value' };
我正在寻找一个简单的方法有marksObject似乎在客户端和节点服务器和同步。 所以如果在客户端或节点服务器上发生改变,改变似乎在两个地方都发生。
理想情况下,这只会涉及到几行代码,我不必写任何东西手动保持客户端和节点服务器同步。
Redis看起来很有趣,但对我所尝试的东西来说似乎是过度杀伤性的。
非常感谢你。
看起来deepstream.io完全是这样对我们用最less的努力。
深度服务器:
var DeepstreamServer = require('deepstream.io'); var server = new DeepstreamServer(); server.set('host', '192.168.123.123'); server.set ('port', 6020 ); server.set ('tcpPort', 6021 ); // this is the default, but its a nice reminder server.start();
节点“客户端”(在服务器上运行,但行为就像一个客户端):
var deepstream = require('deepstream.io-client-js'); ds = deepstream( '192.168.123.123:6021' ).login(); // port has to match tcpPort or just use 6021 record = ds.record.getRecord( 'clientRecord' ); record.set('clientField', 'test from the node client running on the server! '); record.subscribe ('clientField' , function (value) { console.log('subscribed value: ' + value) });
浏览器“客户端”在一个或多个浏览器中运行
<script type='text/javascript' src='deepstream.min.js'></script> <script type='text/javascript'> ds = deepstream( '192.168.123.123:6020' ).login(); record = ds.record.getRecord( 'clientRecord' ); record.set ('clientField', 'test test from a browser!'); record.subscribe ('clientField' , function (value) { console.log('subscribed value: ' + value) }); // for fun, enter record.get('clientField'); in the console too // and also enter record.set('clientField', 'this is what i entered in the console!'); </script>
只需用getter和setter编写一些包装类。 并在“设置”发送更新到另一边。
- 如何解决这个错误与node.js和node-imagemagick:错误:命令失败:execvp():权限被拒绝?
- npm为JavaScript客户端代码
- grunt-spritesmithconfiguration在gruntfile.js上无法正常工作
- date从dd / mm / yyyy转换为yyyy / mm / dd使用javascript
- 带callback的链类方法?
- 使用Hapi&Async的API – 如何清空数组/在“回复”或每个新的“get”之后立即执行?
- .po文件和gettext VS JSON和自定义i18n库?
- NodeJS差异Object.keys(数组).length和array.length
- 我如何获得它之外的节点/摩卡错误的堆栈跟踪