https://www.ddengle.com/develop/2641559
위 사이트 참고했구요
폴로닉스는 잘 받아오는데 비트파이넥스는 어떻게 해야 받아올수 있을까요?
고수님계시면 도움 요청드립니다.
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="theme-color" content="#000000">
<body bgcolor = "#000000" text="#01DF74">
<font size="5px">
<font face="나눔고딕">
<font weight="bold">
<style>
table {border-collapse: collapse}
td, th {padding:5px; width:120px}
</style>
<script src="https://code.jquery.com/jquery-1.4.4.min.js"></script>
<script>
// 전역 변수 세팅
var usd = 0;
var alert_array = new Array();
// 천단위 콤마 함수
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// 숫자 외 문자열 제거 함수
function numberDeleteChar(x) {
return x.toString().replace(/[^0-9]+/g, '');
}
// 폴로닉스 함수
function polo(){
$.get('https://poloniex.com/public?command=returnTicker', function(data) {
var polo_usd = parseFloat(data['USDT_XRP']['last']);
$('#polo_USD').html('' + numberWithCommas((polo_usd) + ' $'));
});
}
// 비트파이넥스 함수
function bitfi(){
$.get('https://api.bitfinex.com/v1/pubticker/xrpusd', function(data) {
var bitfi_usd = parseFloat(data['ask']);
$('#bitfi_USD').html('' + numberWithCommas((bitfi_usd) + ' $'));
});
}
// 현재 시간 갱신
function CurrentTime() {
var d = new Date();
$("#lastUpdate").html(d.toString());
}
// 갱신 함수
function proc() {
try {
polo(); //
bitfi(); //
alert_start(); // 알람 확인
CurrentTime(); // 갱신 시간
} catch(e){
} finally {
setTimeout("proc()", 10000); //10초후 재시작
}
}
</script>
</head>
<body onLoad="proc()">
<!-- 정보 -->
<font size="5px">
<p> 폴로닉스 XRP : <span id="polo_USD"></span> </p>
<p> 비파 XRP : <span id="bitfi_USD"></span> </p>
</body>
</html>
https://https//api.bitfinex.com/v1/pubticker/xrpusd 지금 이렇게 되어 있는데 아래처럼 바꾸니까 나오네요.
https://api.bitfinex.com/v1/pubticker/xrpusd
빗파의 경우 실시간 가격 업데이트 등의 목적으로 웹소켓 방식 호출도 지원하는데, 이 방식을 쓰시면 CORS 문제도 없고, 지금처럼 주기적으로 호출하지 않아도 가격이 실시간으로 업데이트 됩니다.
<script> 태그 안의 코드를 요렇게 바꿔보시면 아마 될 겁니다. (댓글은 들여쓰기가 깨지네요. 붙여넣으실때 잘 정리하시길...)
// 전역 변수 세팅
var usd = 0;
var alert_array = new Array();
// 천단위 콤마 함수
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// 숫자 외 문자열 제거 함수
function numberDeleteChar(x) {
return x.toString().replace(/[^0-9]+/g, '');
}
// 폴로닉스 함수
function polo(){
$.get('https://poloniex.com/public?command=returnTicker', function(data) {
var polo_usd = parseFloat(data['USDT_XRP']['last']);
$('#polo_USD').html('' + numberWithCommas((polo_usd) + ' $'));
});
}
// 현재 시간 갱신
function CurrentTime() {
var d = new Date();
$("#lastUpdate").html(d.toString());
}
// 갱신 함수
function proc() {
try {
polo(); //
alert_start(); // 알람 확인
CurrentTime(); // 갱신 시간
} catch(e){
} finally {
setTimeout("proc()", 10000); //10초후 재시작
}
}
//비트파이넥스 웹소켓
var ws = new WebSocket('wss://api.bitfinex.com/ws/2')
let msg = JSON.stringify({
event: 'subscribe',
channel: 'trades',
symbol: 'tXRPUSD'
})
ws.onopen = function() {
ws.send(msg);
}
ws.onmessage = function(msg) {
var json = JSON.parse(msg.data)
if (!json['event']) {
if (json.length == 2) {
if (json[1].length == 30) {
$('#bitfi_USD').html(json[1][0][3] + ' $');
}
} else if (json.length == 3) {
$('#bitfi_USD').html(json[2][3] + ' $');
}
}
}