Learn R Programming

okxAPI (version 0.1.1)

websocketAPIpublic: websocketAPIpublic Class

Description

Public channel of WebSocket API for Okx exchange v5 API. See Public Channel for more information.

Arguments

Public fields

channel

Public WebSocket url.

simulate

Whether to use demo trading service.

ws

A websocket::WebSocket object to establish a connection to the server.

Methods


Method new()

Create a new websocketAPIpublic object.

Usage

websocketAPIpublic$new(simulate = FALSE)

Arguments

simulate

Whether to use demo trading service.


Method connect()

Initiate the connection to the server.

Usage

websocketAPIpublic$connect()


Method on_open()

Called when the connection is established.

Usage

websocketAPIpublic$on_open(func)

Arguments

func

A Callback function.


Method on_close()

Called when a previously-opened connection is closed. The event will have 'code' (integer) and 'reason' (one-element character) elements that describe the remote's reason for closing.

Usage

websocketAPIpublic$on_close(func)

Arguments

func

A Callback function.


Method on_message()

Called each time a message is received from the server. The event will have a 'data' element, which is the message content.

Usage

websocketAPIpublic$on_message(func)

Arguments

func

A Callback function.


Method on_error()

Called when the connection fails to be established. The event will have an 'message' element, a character vector of length 1 describing the reason for the error.

Usage

websocketAPIpublic$on_error(func)

Arguments

func

A Callback function.


Method send()

Send a message to the server.

Usage

websocketAPIpublic$send(msg)

Arguments

msg

Messages.


Method close()

Close the connection.

Usage

websocketAPIpublic$close()


Method clone()

The objects of this class are cloneable with this method.

Usage

websocketAPIpublic$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

Run this code
if (FALSE) {
tmp <- websocketAPIpublic$new()
tmp$connect()

# subscribe BTC-USDT-SWAP 5m candlesticks data
msg <- list(
  op = "unsubscribe",
  args = list(
    list(channel = "candle5m", instId = "BTC-USDT-SWAP")
  )
)
msg <- jsonlite::toJSON(msg, auto_unbox = TRUE, pretty = TRUE)
tmp$send(msg)

# pass your own callback function
tmp$on_message(function(event) {
  if (event$data == "pong") {
    cat("Bingo!!\n")
  }
})
tmp$send("ping")

tmp$close()
}

Run the code above in your browser using DataLab