我正在尝试使用高速公路订阅poloniex的推送api(加密交换),但是我无法在高速公路会话范围内使用集合插入工作。当集合为空时,插入工作正常 initFakeData方法,但在高速公路会话中不起作用。我的控制台日志显示了这些消息。我没有收到任何错误消息。 我错过了什么吗?
import { DemoCollection } from "../../../both/collections/demo.collection";
import { Demo } from "../../../both/models/demo.model";
import * as autobahn from "autobahn";
export class Main {
start(): void {
this.initFakeData();
}
initFakeData(): void {
if (DemoCollection.find({}).cursor.count() === 0) {
const data: Demo[] = [{
name: "Dotan",
age: 25
}, {
name: "Liran",
age: 26
}, {
name: "Uri",
age: 30
}];
data.forEach((obj: Demo) => {
DemoCollection.insert(obj);
});
}
this.initPoloTroll();
}
initPoloTroll(): void {
let wsuri = "wss://api.poloniex.com";
let connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
connection.onopen = (session) => {
console.log('connection open');
session.subscribe('trollbox', (args, kwargs) => {
console.log({
name: args[2],
age: args[1]
});
DemoCollection.insert({
name: args[2].toString(),
age: args[1]
});
});
}
connection.onclose = function () {
console.log("Websocket connection closed");
}
connection.open();
}
}
答案 0 :(得分:1)
https://www.eventedmind.com/items/meteor-what-is-meteor-bindenvironment
connection.onopen = Meteor.bindEnvironment((session) => {
console.log('connection open');
session.subscribe('trollbox', Meteor.bindEnvironment((args, kwargs) => {
let newObject = {
name: args[2],
age: args[1]
};
console.log(newObject);
DemoCollection.insert(newObject);
console.log('insert done');
}));
});