Created
September 14, 2011 00:10
-
-
Save cronopio/1215530 to your computer and use it in GitHub Desktop.
Playing with WebSocket of Chrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>MtGox Info Test</title> | |
<script type="text/javascript" src="main.js"></script> | |
<style type="text/css"> | |
#main { | |
border:1px green solid; | |
margin: 0 auto; | |
width: 60%; | |
min-height: 200px; | |
} | |
#ticker { | |
border:2px #FA6161 solid; | |
margin: 0 auto; | |
width: 50%; | |
min-height: 50px; | |
} | |
</style> | |
</head> | |
<body> | |
<div id="ticker"></div> | |
<div id="main"></div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Simply JS code for get info from MtGox | |
* using their ws api | |
*/ | |
var ws = new WebSocket('ws://websocket.mtgox.com/mtgox'); | |
ws.onmessage = function(msg){ | |
var info = JSON.parse(msg.data); | |
if (info.channel == 'd5f06780-30a8-4a48-a2f8-7ed181b4a13f'){ | |
console.log('Mensaje en Ticker'); | |
console.log(info.ticker); | |
if (info.ticker) updateTicker(info.ticker); | |
} | |
if (info.channel == 'dbf1dee9-4f2e-4a08-8cb7-748919a71b21'){ | |
console.log('Negocio!'); | |
console.log(info.trade); | |
if (info.trade) addTrade(info.trade); | |
} | |
}; | |
function updateTicker(info){ | |
var c = document.getElementById('ticker'); | |
var div = document.createElement('div'); | |
div.setAttribute('id','msg'); | |
div.setAttribute('class','ticker'); | |
div.innerHTML = 'Compra: '+info.buy+' / Venta: '+info.sell; | |
c.innerHTML = ''; | |
c.appendChild(div); | |
} | |
function addTrade(info){ | |
var m = document.getElementById('main'); | |
var div = document.createElement('div'); | |
var act = ''; | |
div.setAttribute('id','msg'); | |
div.setAttribute('class','trade'); | |
if (info.trade_type == 'ask') act = 'compraron'; | |
if (info.trade_type == 'bid') act = 'vendieron'; | |
div.innerHTML = 'Se '+act+' '+info.amount+' '+info.item+' por '+info.price+' '+info.price_currency; | |
m.appendChild(div); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment