Permalink
Browse files

Fixed issue mentioned by olalonde. Factored console formatting out to…

… separate file. Little cleanup on console output. Put back 'connect' method for better backwards compatibility.
  • Loading branch information...
1 parent 7d0ef8f commit 929b9746fccae2c52d7de520a1fea6507503b38a @johnwarden johnwarden committed Aug 23, 2013
Showing with 22 additions and 107 deletions.
  1. +13 −95 demo.js
  2. +9 −12 mtgox.js
View
@@ -1,12 +1,20 @@
-var util = require('util');
-var colors = require('colors');
-var datetime = require('datetime');
-var MtGoxClient = require('./mtgox').MtGoxClient
+var mtgox = require('./mtgox');
+var consoleFormatting = require('./console-formatting')
+
+var getTradeFormat = consoleFormatting.getTradeFormat;
+var getPriceFormat = consoleFormatting.getPriceFormat;
+var getTickerFormat = consoleFormatting.getTickerFormat;
+var getDepthFormat = consoleFormatting.getDepthFormat;
+var getTimeFormat = consoleFormatting.getTimeFormat;
+
+// var MtGoxClient = require('./mtgox').MtGoxClient
var lastTradePrice = -1;
var lastTickerPrice = -1;
var lastTickerVolume = -1;
-var client = new MtGoxClient();
+
+// var client = new MtGoxClient();
+var client = mtgox.connect();
client.on('connect', function() {
// Good place to unsubscribe from unwanted channels
@@ -65,97 +73,7 @@ var renderDepthMessage = function(depth) {
console.log(getTimeFormat(), getDepthFormat(depth));
};
-var getDepthFormat = function(depth) {
- var format = '';
-
- if (depth.volume < 0) {
- format += '+ '.grey;
- }
- else {
- format += '- '.grey;
- }
-
- if (depth.type_str == 'ask') {
- format += 'Ask: '.grey.bold;
- }
- else if (depth.type_str == 'bid') {
- format += 'Bid: '.grey.bold;
- }
-
- var amount = Math.abs(depth.volume);
- var price = Math.abs(depth.price);
-
- format += (amount + ' ' + depth.item).yellow + ' @ ';
- format += getPriceFormat(price, price, depth.currency);
-
- return format;
-};
-
-var getTickerFormat = function(ticker, lastPrice) {
-
-
- var format = '> ';
-
- var last = 'Last: '.bold;
- var high = 'High: '.bold;
- var low = 'Low: '.bold;
- var vol = 'Vol: '.bold;
- var avg = 'Avg: '.bold;
-
- last += getPriceFormat(ticker.last.display, lastPrice);
- high += ticker.high.display;
- low += ticker.low.display;
- vol += ticker.vol.display;
- avg += ticker.vwap.display;
-
- return format + [vol, high, low, avg, last].join(' ');
-};
-
-var getTradeFormat = function(trade, lastPrice) {
- var format = '$ ';
-
- if (trade.trade_type == 'ask') {
- format += 'Ask: '.bold;
- }
- else if (trade.trade_type == 'bid') {
- format += 'Bid: '.bold;
- }
-
- format += (trade.amount + ' ' + trade.item).yellow + ' @ ';
- format += getPriceFormat(trade.price, lastPrice, trade.price_currency);
-
- return format;
-};
-
var getChannelFormat = function(message) {
var channel = mtgox.getChannel(message.channel)||message.channel;
return channel.name.magenta;
};
-
-var getTimeFormat = function() {
- var now = new Date();
- var time = '[' + datetime.format(now, '%T') + ']';
- return time.blue;
-};
-
-var getPriceFormat = function(currentPrice, lastPrice, currency) {
- var format = currentPrice + (currency ? ' ' + currency : '');
- if (lastPrice < 0) {
- return format;
- }
-
- var delta = lastPrice - currentPrice;
- var percent = (lastPrice > 0) ? (delta / lastPrice) * 100 : 100;
- var round = function(n) {
- return Math.round(Math.abs(n) * 100) / 100;
- };
-
- if (delta > 0) {
- format += (' \u25b2 +' + round(delta) + ' +' + round(percent) + '%').green;
- }
- else if (delta < 0) {
- format += (' \u25bc -' + round(delta) + ' -' +round( percent) + '%').red;
- }
-
- return format;
-};
View
@@ -20,7 +20,7 @@ var MTGOX_CHANNELS = [
var getChannel = function(key) {
return MTGOX_CHANNELS.filter(function(channel) {
- return (channel.key == key || channel.private == key || channel.key == key);
+ return (channel.key == key || channel.private == key);
})[0];
};
@@ -55,16 +55,12 @@ var MtGoxClient = function(options) {
socket.on('message', function(raw) {
lastPulse = new Date() / 1E3;
- // Emit raw data
var data = raw;
- // Emit messages
var message = data;
-
if (message.op == 'remark') {
console.info("Remark",message);
- console.info("Debug",message.debug);
}
else if (message.op == 'subscribe') {
console.info("Got subscribe confirmation",getChannel(message.channel).name);
@@ -109,11 +105,11 @@ var MtGoxClient = function(options) {
});
socket.on('connect', function() {
- console.info("Got connect");
+ console.info("Successfully connected");
if(options.channels) {
if(options.channels.client) {
- console.info("Subscribing to client");
+ console.info("subscribing to private client data");
self.subscribePrivate();
}
if(!options.channels.trades) {
@@ -126,7 +122,6 @@ var MtGoxClient = function(options) {
}
}
-
});
socket.on('disconnected', function() {
@@ -140,7 +135,7 @@ var MtGoxClient = function(options) {
socket.on('close', function() {
- console.info("Got close");
+ console.info("Got socket close");
});
function reSubscribe() {
@@ -215,10 +210,10 @@ var MtGoxClient = function(options) {
function checkPulse() {
var now = new Date() / 1E3;
if((now - lastPulse) > 30) {
- console.info("checking pulse: " + Math.round((now - lastPulse)*100)/100);
+ console.info("No activity on socket for " + (Math.round((now - lastPulse)*100)/100) + "s");
}
if (now - lastPulse > threshhold) {
- console.info("More than " + threshhold + "s since last data. Reopening socket.")
+ console.info("More than " + threshhold + "s since last activity. Destroying and re-creating socket.")
reopenSocket();
}
}
@@ -228,4 +223,6 @@ util.inherits(MtGoxClient, EventEmitter);
exports.MtGoxClient = MtGoxClient;
-
+exports.connect = function() {
+ return new MtGoxClient();
+}

0 comments on commit 929b974

Please sign in to comment.