我正在寻找开发一个依赖于与外部服务API交谈的新应用程序.
例如,我想创建一个实时的Twitter提要,每次有新的推文时都会更新,我想使用Meteor作为框架,但我不确定是否可以让Meteor在没有页面刷新的情况下自动显示新的推文.
我知道我可以用Node.js和Socket.io做到这一点,但是可以单独使用Meteor吗?
谢谢
基本上有两种从外部源检索数据的方法.服务器上的Ajax或http请求.我最近解决了这个问题,但不得不使用第二种方法.
Client.js
Meteor.startup( function() {
Meteor.call( 'openSession', function( err, res ) {
if( !err ) Session.set( 'data', res );
});
});
Run Code Online (Sandbox Code Playgroud)
Server.js
Meteor.methods({
openSession: function() {
var fut = new Future(), url = 'http://www.google.com';
// Do call here, return value with Future
Meteor.http.get(url, function( err, res ){
fut.ret(res);
});
// Force method to wait on Future return
return fut.wait();
}
});
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我不得不使用Future来使Meteor与异步http请求一起播放.但是,它就像在服务器端定义方法一样简单,然后在客户端调用它.
| 归档时间: |
|
| 查看次数: |
3065 次 |
| 最近记录: |