I am using the same method to connect to the websocket server as here https://cran.r-project.org/web/packages/websocket/readme/README.html
However, the output has the following structure using ws$connect()
function
> ws$connect()
Connection opened
send login request
Got Message [{"ID":1,"Type":"Refresh","Domain":"Login","Key":{"Name":"XXXX","Elements":{"AllowSuspectData":1,"ApplicationId":"XXX","ApplicationName":"XXX","AuthenticationErrorCode":XXX,
"AuthenticationErrorText":{"Type":"AsciiString","Data":null},"AuthenticationTTReissue":XXX,"Position":"XXX","ProvidePermissionExpressions":1,"ProvidePermissionProfile":0,"SingleOpen":1,"SupportEnhancedSymbolList":1,"SupportOMMPost":1,"SupportPauseResume":0,"SupportStandby":0,"SupportBatchRequests":7,"SupportViewRequests":1,"SupportOptimizedPauseResume":0}},"State":{"Stream":"Open","Data":"Ok","Text":"Login accepted by host XXX"},"Elements":{"PingTimeout":XX,"MaxMsgSize":XXX}}]
#here starts part, which is needed to be saved (Price1-3)
Single Message
{"ID":2,"Key":{"Name":"XXX"},"View":["Price1","Price2","Price3"]}
Got Message [{"ID":2,"Type":"Refresh","Key":{"Service":"XXX","Name":"XXX"},"State":{"Stream":"Open","Data":"Ok","Text":"*All is well"},"Qos":{"Timeliness":"XXX","Rate":"XXX"},"PermData":"XXX","SeqNumber":4000,"Fields":{"Price1":16.255,"Price2":16.345,"Price3":5}}]
The $onMessage()
function is following (with # I marked the lines I added, source: https://github.com/Refinitiv/websocket-api/blob/master/Applications/Examples/R/market_price_authentication.R )
test1<-NULL
ws$onMessage(function(event) {
cat("Got Message",event$data,"\n")
jsonresp <- fromJSON(event$data, simplifyDataFrame=FALSE)
#test1<-event$data
#event$data
for (singleMsg in jsonresp){
process_message(singleMsg) # Single Message send pong
}
}
)
How can I save the output of this function?
I was working on the same problem
find out a solution with shiny:
library(httpuv)
library(promises)
library(websocket)
library(shiny)
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(position = "right",
sidebarPanel("sidebar panel"),
mainPanel(
textOutput("selected_var")
)
)
)
server <- function(input, output) {
MSG <- reactiveValues()
s <- startServer("0.0.0.0", 8081,
list(
onHeaders = function(req) {
# Print connection headers
cat(capture.output(str(as.list(req))), sep = "\n")
},
onWSOpen = function(ws) {
cat("Connection opened.\n")
ws$onMessage(function(binary, message) {
print(message)
MSG$A <- message
ws$send(message)
})
ws$onClose(function() {
cat("Connection closed.\n")
})
}
)
)
output$selected_var <- renderText({
paste("Grasshopper Sended:", MSG$A)
})
}
shinyApp(ui = ui, server = server)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.