0

I'm trying to get data from websocket of this page https://upbit.com/exchange?code=CRIX.UPBIT.KRW-BTC , but it seems its websocket URL is dynamic and can NOT be reused, the second time I connect to it, it doesn't send data to me anymore, only the first connection is valid.

enter image description here

For now, I found a function in this JS file https://cdn.upbit.com/vender-bundle-03c1ef05e3ad23870adb.js,

var a = this,
c = o.addPath(t, "/websocket");
c = "https" === c.slice(0, 5) ? "wss" + c.slice(5) : "ws" + c.slice(4), 
this.url = c, this.ws = new u(this.url, [], n)

There are thousands of functions with the same name n(), so I'm clueless here, any ideas? Thanks!

soulmachine
  • 3,917
  • 4
  • 46
  • 56

1 Answers1

3

You don't need a dynamic url in this case. You can just pick one and use it as described in the answer in below answer

https://stackoverflow.com/a/48457699/2830850

const WebSocket = require("ws")
const ws = new WebSocket("wss://example.com/sockjs/299/enavklnl/websocket",null,{
    headers: {
        "Cookie":"<cookie data noted earlier>",
        "User-Agent": "<Your browser agent>"
    },
    origin: "https://example.com",
})
const opening_message = '["[{\\"ticket\\":\\"ram macbook\\"},{\\"type\\":\\"recentCrix\\",\\"codes\\":[\\"CRIX.UPBIT.KRW-BTC\\",\\"CRIX.BITFINEX.USD-BTC\\",\\"CRIX.BITFLYER.JPY-BTC\\",\\"CRIX.OKCOIN.CNY-BTC\\",\\"CRIX.KRAKEN.EUR-BTC\\",\\"CRIX.UPBIT.KRW-DASH\\",\\"CRIX.UPBIT.KRW-ETH\\",\\"CRIX.UPBIT.KRW-NEO\\",\\"CRIX.UPBIT.KRW-BCC\\",\\"CRIX.UPBIT.KRW-MTL\\",\\"CRIX.UPBIT.KRW-LTC\\",\\"CRIX.UPBIT.KRW-STRAT\\",\\"CRIX.UPBIT.KRW-XRP\\",\\"CRIX.UPBIT.KRW-ETC\\",\\"CRIX.UPBIT.KRW-OMG\\",\\"CRIX.UPBIT.KRW-SNT\\",\\"CRIX.UPBIT.KRW-WAVES\\",\\"CRIX.UPBIT.KRW-PIVX\\",\\"CRIX.UPBIT.KRW-XEM\\",\\"CRIX.UPBIT.KRW-ZEC\\",\\"CRIX.UPBIT.KRW-XMR\\",\\"CRIX.UPBIT.KRW-QTUM\\",\\"CRIX.UPBIT.KRW-LSK\\",\\"CRIX.UPBIT.KRW-STEEM\\",\\"CRIX.UPBIT.KRW-XLM\\",\\"CRIX.UPBIT.KRW-ARDR\\",\\"CRIX.UPBIT.KRW-KMD\\",\\"CRIX.UPBIT.KRW-ARK\\",\\"CRIX.UPBIT.KRW-STORJ\\",\\"CRIX.UPBIT.KRW-GRS\\",\\"CRIX.UPBIT.KRW-VTC\\",\\"CRIX.UPBIT.KRW-REP\\",\\"CRIX.UPBIT.KRW-EMC2\\",\\"CRIX.UPBIT.KRW-ADA\\",\\"CRIX.UPBIT.KRW-SBD\\",\\"CRIX.UPBIT.KRW-TIX\\",\\"CRIX.UPBIT.KRW-POWR\\",\\"CRIX.UPBIT.KRW-MER\\",\\"CRIX.UPBIT.KRW-BTG\\",\\"CRIX.COINMARKETCAP.KRW-USDT\\"]},{\\"type\\":\\"crixTrade\\",\\"codes\\":[\\"CRIX.UPBIT.KRW-BTC\\"]},{\\"type\\":\\"crixOrderbook\\",\\"codes\\":[\\"CRIX.UPBIT.KRW-BTC\\"]}]"]'
ws.on('open', function open() {
    console.log("opened");
});

ws.on('message', function incoming(data) {
    if (data == "o" || data == "h") {
        console.log("sending opening message")
        ws.send(opening_message)
    }
    else {
        console.log("Received", data)

    }
});
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • 1
    Amazing answer, and @Tarun has an excellent analysis under another question here https://stackoverflow.com/a/48457699/381712 – soulmachine Jan 27 '18 at 19:45
  • There is only one little issue though, I don't want to hard code this URL `wss://example.com/sockjs/299/enavklnl/websocket` in my code, I want to get it automatically. – soulmachine Jan 27 '18 at 19:46
  • @soulmachine, I think the url generated is just random and has probably logic that impacts the data. So you can yourself generated a random num and string and then try, if you don't want to hardcode – Tarun Lalwani Jan 28 '18 at 14:44