实时开发框架Meteor API解读系列<六> DDP

本文详细解析Meteor的DDP连接机制,通过DDP.connect(url)方法实现不同Meteor应用之间的数据订阅和远程方法调用。文章通过实例演示了如何在客户端和服务端建立连接,并介绍了status、subscribe、methods和onReconnect等关键方法的用法,强调了数据安全性和Meteor.publish的数据限制策略。
摘要由CSDN通过智能技术生成

写在前面的话

  1. 本篇博客主要讲 DDP
  2. 使用的meteor版本为0.8.0
  3. 老话重提 ,尽量以官方文档为主
  4. 本篇博客有不正确的地方在所难免,如有发现望指出。
  5. 本篇博客地址: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的文件结构:

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值