Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中断时自动重新连接到websocket?_R_Websocket - Fatal编程技术网

如何在R中断时自动重新连接到websocket?

如何在R中断时自动重新连接到websocket?,r,websocket,R,Websocket,我正在连接到websocket并处理数据,但不可避免地会发生连接断开。下面的代码演示了如何设置连接。我希望这样,当连接断开时,我的代码会重新启动 library(websocket) library(data.table) library(RJSONIO) wsURL <- "wss://ftx.com/ws/" ws <- WebSocket$new(wsURL) StartSocket <- function() { ticker <- 'B

我正在连接到websocket并处理数据,但不可避免地会发生连接断开。下面的代码演示了如何设置连接。我希望这样,当连接断开时,我的代码会重新启动

library(websocket)
library(data.table)
library(RJSONIO)

wsURL <- "wss://ftx.com/ws/"
ws <- WebSocket$new(wsURL)
StartSocket <- function() {
  ticker <- 'BTC-PERP'
    
  req <- list(
    market = ticker,
    channel = 'trades',
    op = 'subscribe')
  
  reqJson <- toJSON(req)
    
  ws$send(reqJson)
  
  ws$onMessage(function(event) {
    data <- fromJSON(event$data)
    print(data)
  })
}
Stop <- function() {
  ws$close()
}  
StartSocket()
#Stop()
库(websocket)
库(数据表)
图书馆(RJSONIO)
wsURL