写在前面的话
- 本篇博客主要讲 DDP
- 使用的meteor版本为0.8.0
- 老话重提 ,尽量以官方文档为主
- 本篇博客有不正确的地方在所难免,如有发现望指出。
- 本篇博客地址:http://blog.csdn.net/a6383277/article/details/23656929 转载请注明出处,谢谢
DDP
这个类只有个方法 DDP.connect(url),先来看看这个类的作用是什么。
官方文档是这样说的:
Connect to the server of a different Meteor application to subscribe to its document sets and invoke its >remote methods.
大概翻译一下:连接不同的Meteor应用 订阅它的数据集而且调用它的远程方法。从而可以通过一个公开的数据集合订阅构建自己的应用。等下通过代码来解释。
现在看是使用
DDP.connect(url) @Anywhere
这个函数可以在客户端或者服务端使用。
参数url是你需要订阅的另个Meteor应该的连接地址。这个函数会返回一个对象,这个对象中包含一下方法(这些方法不是都能在客户端和服务端同时调用的):
subscribe - 订阅一个数据集
call - 调用函数. 具体见: Meteor.call.
apply - 调用函数. 具体见:Meteor.apply.
methods - Define client-only stubs for methods defined on the remote server. See Meteor.methods.
status - 当前连接状态. 具体见: Meteor.status.
reconnect - 具体见:Meteor.reconnect.
disconnect - 具体见: Meteor.disconnect.
onReconnect - Set this to a function to be called as the first step of reconnecting. This function can call methods which will be executed before any other outstanding methods. For example, this can be used to re-establish the appropriate authentication context on the new connection.
(ps:http://stackoverflow.com/questions/18358526/connect-two-meteor-applications-using-ddp#)
先看代码。由于这个东西是需要链接其它meteor应用,因此需要写两个meteor应用。被链接的应用 给个名字”main-server”,主动去连接的”deputy-server”
main-server
先看main-server的文件结构: