【发布时间】:2016-07-18 10:58:58
【问题描述】:
在下面的代码中,我希望从提要接收“订单”并将其存储在数据库中。
我知道只要收到数据就会调用类方法 marketEvent,因此我需要在这个函数中插入我的语句。
每次调用类方法时打开和关闭 db 连接效率低下,所以我试图打开连接并将 db 对象传递给 marketEvent。
我是 nodejs 和 web 套接字的新手,所以不知道如何将它们组合在一起。
var pg = require("pg")
var conString = "postgres://myusername:mypassword@localhost/poloniex";
var client = new pg.Client(conString);
var autobahn = require('autobahn');
var wsuri = "wss://api.poloniex.com";
var connection = new autobahn.Connection({
url: wsuri,
realm: "realm1"
});
connection.onopen = function (session) {
function marketEvent (args,kwargs) {
client.query("INSERT INTO orderbook(order) values($1)", [args]);
}
session.subscribe('BTC_XMR', marketEvent);
}
connection.onclose = function () {
console.log("Websocket connection closed");
}
client.connect();
connection.open();
【问题讨论】:
标签: node.js postgresql autobahn