简体   繁体   English

通过web套接字连接字符串从我自己的express服务器连接到第三方服务器(twelvedata)

[英]Connect to a third party server (twelvedata) from my own express server through web socket connection string

I want to connect to the twelevedata server through its provided socket connection to receive information.我想通过其提供的套接字连接连接到twelevedata服务器以接收信息。


import * as dotenv from 'dotenv' 
import WebSocket from 'ws';
import express from 'express'

const app = express();

//setting up env
dotenv.config()

// setting up the websocket
const ws = new WebSocket(`wss://ws.twelvedata.com/v1/quotes/price?apikey=${process.env.API_KEY_TWELVEDATA}`);


const payload = {
  "action": "subscribe",
  "params": {
  "symbols": "AAPL,INFY,TRP,QQQ,IXIC,EUR/USD,USD/JPY,BTC/USD,ETH/BTC"
  },
}




ws.on('connection',function (steam) {

  ws.on('open', (data) => {
    console.log("data ==>",data);
    ws.emit('subscribe',payload)
  })
    
  ws.on('subscribe', (data) => {
    console.log("data ==>",data);
  })
})

const port = process.env.PORT || 5000;
app.listen(port, () => {
  console.log(`I am listening at ${port}`);
});

I created a websocket with my websocket connection on an express application but I am unable to receive any information from the twelvedata server regarding the subscribe event that I have emitted !我在一个快速应用程序上用我的 websocket 连接创建了一个 websocket,但是我无法从 twelvedata 服务器接收到关于我发出的subscribe事件的任何信息!

This is how the websocket should work as shown by the twelvedata website (look into the screen shots)这就是 websocket 应该如何工作,如twelvedata网站所示(查看屏幕截图)

在此处输入图像描述 在此处输入图像描述

I am unable to connect and emit the subscribe event given by the twelvedata's documentation我无法连接并发出twelvedata 文档给出的subscribe事件

You don't want to emit events from the websocket (that's for events you want to handle locally), but send , ie, replace您不想从 websocket emit事件(这是您要在本地处理的事件),而是send ,即替换

ws.emit('subscribe',payload)

with

ws.send(payload)

// sending the parameters
 ws.on('open', function open() {
    ws.send(JSON.stringify(payload));
  });
  
  ws.on('message', function message(data) {
    // receiving data
    console.log('data ===>: ', JSON.parse(data));
  });

ws.send did the charm for me ws.send为我带来了魅力

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 node.js 发起与 Twelvedata 服务器的 Socket 连接 - Initate Socket connection with Twelvedata server in node js Socket.IO启动从客户端到第三方服务器的连接 - Socket.IO start a connection from client to a third-party server 获取API。 我从我自己的快递服务器得到一个空字符串 - FetchAPI . Im getting an empty string from my own express server 如何将我从第三方 API 获取的数据存储在我的快速服务器中,并在以后安排另一个 API 调用? - How to store the data I've fetched from a third-party API in my express server and schedule another API call at a later time? 使用 node & express 从我自己的服务器提供任何外部网页 - Serve any external web page from my own server using node & express 缓慢的第三方 API 阻塞了 Express 服务器 - Slow third-party APIs clogging Express server 为什么使用Socket.IO将NodeJS服务器(Sails)连接到另一个NodeJS Server(Express4)失败? - Why is my connection of a NodeJS server (Sails) to another NodeJS Server (Express4) using Socket.IO failing? 在Linux服务器上的项目文件夹中使用node js express和socket.io创建套接字连接时出错 - Error creating socket connection using node js express and socket.io in my project folder on linux server 无法通过安全 (https) 连接连接到我的 AWS 节点服务器 - Cant connect to my AWS node server through secure (https) connection socket.io服务器未监听连接事件(快速服务器) - socket.io server not listening to connection event (express server)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM