Procházet zdrojové kódy

Added WebSocket connection

Efren Yevale Varela před 4 roky
rodič
revize
570d82926c
1 změnil soubory, kde provedl 40 přidání a 0 odebrání
  1. 40 0
      app.js

+ 40 - 0
app.js

@@ -0,0 +1,40 @@
+const websocket = new Vue({
+  el: "#websocket",
+  created: () => {
+    this.socket = new WebSocket("wss://api-pub.bitfinex.com/ws/2")
+    this.socket.onopen = () => {
+      websocket.connected = true
+    };
+    this.socket.onclose = () => {
+      websocket.info      = "";
+      websocket.connected = false;
+    };
+    this.socket.onmessage = (message) => {
+      json = JSON.parse(message.data);
+      if (json.event == "info") websocket.info = `Server ${json.serverId} v${json.version}`;
+      if (Array.isArray(json) && json[1] !== "hb") websocket.processValues(json);
+    };
+  },
+  data: {
+    connected: false,
+    info:      "",
+    socket:    undefined
+  },
+  methods: {
+    processCandle: (candle) => {
+      console.log(candle);
+    },
+    processValues: (json) => {
+      if (Array.isArray(json[1][0])) {
+        json[1].forEach((data) => websocket.processCandle(data));
+      } else {
+        websocket.processCandle(json[1]);
+      }
+    }
+  },
+  watch: {
+    connected: (connected) => {
+      if (connected === true) this.socket.send('{"event":"subscribe","channel":"candles","key":"trade:1m:tBTCUSD"}');
+    }
+  }
+});