基本上,我正在尝试从 Poloniex 获取 wss 提要,并使用它更新集合,以便我可以在集合中获得“最新”价格(我将更新并覆盖现有条目)并将其显示在网页上。现在,我让 wss 工作,我只是想在集合中插入一些数据以查看它是否工作,但它没有工作,我不知道为什么!
注意:该集合有效,我已使用外壳手动插入一条记录。
这是我现在拥有的代码:
import { Meteor } from 'meteor/meteor';
import * as autobahn from "autobahn";
import { Mongo } from 'meteor/mongo'
import { SimpleSchema } from 'meteor/aldeed:simple-schema'
//quick DB
Maindb = new Mongo.Collection('maindb');
Maindb.schema = new SimpleSchema({
place: {type: String},
pair: {type: String},
last: {type: Number, defaultValue: 0}
});
Meteor.startup(() => {
var wsuri = "wss://api.poloniex.com";
var Connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
Connection.onopen = function(session)
{
function tickerEvent (args,kwargs) {
console.log(args[0]);
Maindb.insert({place: 'Poloniex', pair: args[0]});
}
session.subscribe('ticker', tickerEvent);
Connection.onclose = function () {
console.log("Websocket connection closed");
}
}
Connection.open();
});
控制台记录提要,但插入不起作用。我在网上查看,它说要在“非 Meteor”功能中使插入工作,您需要使用Meteor.bindEnvironment
我所做的:
我变了
function tickerEvent (args,kwargs) {
console.log(args[0]);
Maindb.insert({place: 'Poloniex', pair: args[0]});
}
这成为
var tickerEvent = Meteor.bindEnvironment(function(args,kwargs) {
console.log(args[0]);
Maindb.insert({place: 'Poloniex', pair: args[0]});
}); tickerEvent();
什么都不做——甚至不在我的控制台上打印提要。使用相同的结构,但只是将Meteor.bindEnvironment
打印再次删除到控制台,但不会更新。
难道我做错了什么?