Mongo Collection插入不工作的流星服务器



我正在尝试使用Autobahn订阅Poloniex(加密交易所(的推动API,但是我很难在Autobahn会话的范围内获得收集插件来工作。当收集为空时,插入正常工作initfakedata方法,但在Autobahn会话中不起作用。我的控制台日志显示了消息。我没有收到任何错误消息。我想念什么吗?

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();
  }
}

https://www.eventedmind.com/items/meteor-what-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');
      }));
    });

最新更新