使用带有webpack的socket.io连接到GDAX websocket api [英] Connecting to GDAX websocket api using socket.io with webpack

查看:177
本文介绍了使用带有webpack的socket.io连接到GDAX websocket api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用一个由react和webpack构建的浏览器应用程序连接到GDAX websocket api.我不能使用官方的gdax-node或gdax-toolkit api,因为它们与webpack不兼容.我决定尝试使用socket.io自己连接到websocket,但是下面的代码从未建立连接.在连接之后,我的订阅"日志消息下面的代码永远不会出现.如何获取此代码以连接或至少显示错误消息?

I want to connect to the GDAX websocket api with an in browser application built with react and webpack. I cannot use the offiicial gdax-node or gdax-toolkit api's because they are not compatible with webpack. I decided to try connecting to the websocket myself using socket.io, but the code below never establishes a connection. In the code below my "subscribing" log message after connect never appears. How do I get this code to connect or at least show an error message?

const io = require('socket.io-client');

var subscribe = {
  "type": "subscribe",
  "channels": [{"name": "ticker", "product_ids": ["BTC-EUR"]}]
};

function subscribeToTimer(cb) {
  console.log('Opening socket');
  var socket = io.connect('wss://ws-feed.gdax.com');

  socket.on('connection', function(socket) {
    console.log('Subscribing');

    socket.on('disconnect', function(socket) {
      console.log('Clinet disconnected.');
    });    
  });

  //socket.on('message', timestamp => cb(null, timestamp));
  socket.on('message', data => { console.log(data); });
  socket.on('error', data => { console.log(data); });
}
export { subscribeToTimer };

推荐答案

由于gdax仅向公共公开了wss网址,而socket.io不支持wssws连接,因此我们必须找到解决方法.在这里global.websocket足以在浏览器中建立连接.或者,您也可以查看图书馆.它只是包装了ws软件包,当您使用webpack构建供浏览器使用时,它将用browser.js替换主软件包.

Since gdax only exposed the wss url to public and socket.io does not support wss or ws connection, we have to find work around. Here global.websocket is enough to make connection in the browser. Or you could also check out this library. It simply wraps the ws package and when you build by webpack for browser usage, it will replace the main by browser.js.

这篇关于使用带有webpack的socket.io连接到GDAX websocket api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆