R语言WebSocket连接成功但无法接收FinancialModelingPrep消息求助
R语言复现FinancialModelingPrep WebSocket脚本无法接收实时行情数据
我尝试用R语言复现一段NodeJS的WebSocket脚本,已经成功和FinancialModelingPrep的WebSocket服务建立连接,但始终收不到预期的实时行情数据。
原NodeJS参考脚本
const WebSocket = require('ws'); const ws = new WebSocket('wss://websockets.financialmodelingprep.com', null, null, null, {rejectUnauthorized: false}); const login = { 'event':'login', 'data': { 'apiKey': "<apiKey>", } } const subscribe = { 'event':'subscribe', 'data': { 'ticker': "aapl", } } ws.on('open', function open() { ws.send(JSON.stringify(login)); ws.send(JSON.stringify(subscribe)); }); ws.on('message', function(data, flags) { console.log(data) });
我的R语言实现(使用websocket库)
login <- list( 'event' = 'login', 'data' = list( 'apiKey' = "<apiKey>" ) ) subscribe <- list( 'event' = 'subscribe', 'data' = list( 'ticker' = "btcusd" ) ) unsubscribe <- list( 'event' = 'unsubscribe', 'data' = list( 'ticker' = "btcusd" ) ) ws <- websocket::WebSocket$new( url = 'wss://crypto.financialmodelingprep.com', protocols = character(0), headers = NULL, autoConnect = F, accessLogChannels = c("all"), maxMessageSize = 32 * 1024 * 1024 ) ws$onOpen(function(event) { cat("Connection opened\n") ws$send(toJSON(login)) cat("Login in then pause...\n") Sys.sleep(1) ws$send(toJSON(subscribe)) cat("Subscribe then pause...\n") Sys.sleep(1) cat("Unsubscribing...\n") ws$send(toJSON(unsubscribe)) }) ws$onMessage(function(event) { cat("Client got msg: ", fromJSON(event$data), "\n") }) ws$onClose(function(event) { cat("Client disconnected with code ", event$code, " and reason ", event$reason, "\n", sep = "") }) ws$onError(function(event) { cat("Client failed to connect: ", event$message, "\n") }) ws$connect()
运行日志输出
Connection opened Login in then pause... [2022-11-19 12:05:47] [frame_header] Dispatching write containing 1 message(s) containing 6 header bytes and 74 payload bytes [2022-11-19 12:05:47] [frame_header] Header Bytes: [0] (6) 81 CA DF B7 07 F8 [2022-11-19 12:05:47] [frame_payload] Payload Bytes: [0] (74) [1] ��b���s���%���n���+ڻ�s���|ھ�n���%�0���4��aλ�aͻ�4��2���a���4���%��� Subscribe then pause... [2022-11-19 12:05:48] [frame_header] Dispatching write containing 1 message(s) containing 6 header bytes and 52 payload bytes [2022-11-19 12:05:48] [frame_header] Header Bytes: [0] (6) 81 B4 05 00 D3 E9 [2022-11-19 12:05:48] [frame_payload] Payload Bytes: [0] (52) [1] ~"��`n��?[�pb��wi��']��aa��':��qi��`r��^"��fu��']�� Unsubscribing... [2022-11-19 12:05:49] [frame_header] Dispatching write containing 1 message(s) containing 6 header bytes and 54 payload bytes [2022-11-19 12:05:49] [frame_header] Header Bytes: [0] (6) 81 B6 E1 AF 95 38 [2022-11-19 12:05:49] [frame_payload] Payload Bytes: [0] (54) [1] ���N������M���Z���Q�ʷe͍�Y�η���Q���JÕ����M�˷e�� [2022-11-19 12:06:49] [error] handle_read_frame error: websocketpp.transport:7 (End of File) [2022-11-19 12:06:49] [disconnect] Disconnect close local:[1006,End of File] remote:[1006] Client disconnected with code 1006 and reason
预期接收的消息格式(来自FinancialModelingPrep文档)
{ "s": "btcusd", "t": 16487238632060000000, "e": "binance", "type": "Q", "bs": 0.00689248, "bp": 47244.8, "as": 1.72784126, "ap": 47244.9 }
我不确定问题出在哪里,而且没有NodeJS开发经验无法改用该语言实现,希望能得到解决方案。
内容的提问来源于stack exchange,提问作者Keith Cozart
相关产品推荐
相关产品推荐

