我找到了自己的解决方案。这是一个最基本的实现呼叫从一个Chrome扩展流星服务器:
的manifest.json { “manifest_version”:2
, "name": "DDP Test"
, "version": "0.1"
, "background": {
"scripts": [
"jquery-3.1.0.js"
, "meteor-ddp.js"
, "meteor.js"
, "background.js"
]
}
}
jQuery的3.1.0.js可发现here
eddfirs流星ddp.js可以发现here
meteor.js(改编自Meteor Spotting)
var Meteor
;(function meteor(){
"use strict"
var endpoint = "ws://localhost:3000/websocket"
// Use your own endpoint ^^^
Meteor = {
call : function(){
var args = [].slice.call(arguments)
var methodName = args.shift()
var callback = args.pop()
var ddp = new MeteorDdp(endpoint)
if (!(callback instanceof Function)) {
args.push(callback)
callback = function (error, data) {
console.log("Result of "+methodName+" call:", error, data)
}
}
ddp.connect().done(MeteorCall)
function MeteorCall() {
console.log('Connected!');
var $deferred = ddp.call(methodName, args)
$deferred.done(function (result) {
callback(null, result)
})
$deferred.fail(function (error) {
callback(error)
})
}
}
};
})()
个
background.js
;(function background(){
"use strict"
Meteor.call("test", 1, "two", { three: [4]})
Meteor.call("test", "using", "a", "custom", callback)
function callback(error, result) {
console.log("Custom callback error:", error, ", Result:", result)
}
})()
输出在服务器控制台当扩展被加载:
I20160917-19:35:19.352(-4)? test 1 two { three: [ 4 ] }
I20160917-19:35:19.377(-4)? test using a custom
输出在检查用于背景视图:
Connected!
meteor.js:18 Result of test call: null test method activated with arguments {"0":1,"1":"two","2":{"three":[4]}}
meteor.js:25 Connected!
background.js:8 Custom callback error: null , Result: test method activated with arguments {"0":"using","1":"a","2":"custom"}