[英]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.