library(websocket) library(jsonlite) library(httr) REFINITIV_API_URL <- "https://api.refinitiv.com/" OAUTH2_TOKEN_URL <- paste(REFINITIV_API_URL, "auth/oauth2/v1/token", sep="") RELOGIN_INTERVAL = 250 NEXT_RELOGIN_TIME = Sys.time() SEND_REQUEST=FALSE USERNAME <- "username" PASSWORD <- "password" CLIENTID <- "clientid" if(.Platform$OS.type == "unix") { config = system("ifconfig", intern=TRUE) ips = config[grep("inet addr", config)] if(length(ips) == 0) { ips = config[grep("inet ", config)] positions = strsplit(gsub(".*inet ([[:digit:]])", "\\1", ips), " ") first_positions = lapply(positions, '[[', 1) position = first_positions[first_positions != "127.0.0.1"][[1]] } else { position = strsplit(gsub(".*inet addr:([[:digit:]])", "\\1", ips), " ")[[1]][[1]] } } else { config = system("ipconfig", intern=TRUE) ips = config[grep("IPv4", config)] position = strsplit(gsub(".*? ([[:digit:]])", "\\1", ips), " ")[[1]][[1]] } cat(position, "\n") get_access_token = function(){ cat("get_access_token") cat("\n") RequestTokenBody <- list(grant_type="password", username=USERNAME, password=PASSWORD, scope="trapi", takeExclusiveSignOnControl= "True", client_id=CLIENTID ) RequestTokenResponse <- httr::POST(OAUTH2_TOKEN_URL, add_headers(Authorization = CLIENTID), body = RequestTokenBody, encode = "form") TokenContent <- httr::content(RequestTokenResponse, "parsed", "application/json", encoding="UTF-8") NEXT_RELOGIN_TIME <<- Sys.time() + RELOGIN_INTERVAL return (TokenContent$access_token) } ACCESS_TOKEN = get_access_token() ws <- WebSocket$new("wss://emea-1.pricing.streaming.edp.thomsonreuters.com:443/WebSocket", protocol="tr_json2", autoConnect = FALSE) ws$onOpen(function(event) { cat("Connection opened\n") send_login_request(ws, ACCESS_TOKEN) }) send_login_request = function(con, access_token){ cat("send login request") cat("\n") message_array <- c("{\"Domain\":\"Login\",\"ID\":1,\"Key\":{\"Elements\":{\"ApplicationId\":\"256\",\"AuthenticationToken\":\"",access_token,"\",\"Position\":\"",position,"\"}, \"NameType\":\"AuthnToken\"}}") login_req <- paste(message_array, collapse="") con$send(login_req) } send_market_price_request = function(con) { mp_req_json_string = "{\"ID\":2,\"Key\":{\"Name\":\"JPY=\"},\"View\":[\"BID\",\"ASK\"]}" cat(mp_req_json_string) cat("\n") con$send(mp_req_json_string) } process_message = function(message_json) { cat("Single Message","\n") message_type = message_json[['Type']] if(message_type == "Refresh") { message_domain = message_json[['Domain']] if(!is.null(message_domain)) { if(message_domain == "Login") { if(SEND_REQUEST==FALSE){ send_market_price_request(ws) SEND_REQUEST<<-TRUE } } } }else if (message_type == "Ping"){ pong_json_string = "{\"Type\":\"Pong\"}" cat("send pong","\n") ws$send(pong_json_string) } } ws$onMessage(function(event) { cat("Got Message",event$data,"\n") jsonresp <- fromJSON(event$data, simplifyDataFrame=FALSE) for (singleMsg in jsonresp){ process_message(singleMsg) } } ) 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() while(TRUE){ httpuv::service(250000) if(Sys.time() >= NEXT_RELOGIN_TIME){ cat(Sys.time(), "\n") cat(NEXT_RELOGIN_TIME, "\n") ACCESS_TOKEN = get_access_token() send_login_request(ws, ACCESS_TOKEN) } }