【问题标题】:How save the output messages in the console in RStudio?如何在 RStudio 的控制台中保存输出消息?
【发布时间】:2021-01-11 09:38:13
【问题描述】:

我使用与这里https://cran.r-project.org/web/packages/websocket/readme/README.html相同的方法连接到websocket服务器

但是,使用ws$connect() 函数的输出具有以下结构

> 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}}] 

$onMessage() 函数如下(用# 标记了我添加的行,来源: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
   
 }  
 
}
)

如何保存这个函数的输出?

【问题讨论】:

  • 从在线文档看来,您似乎需要重写 onMessage() 函数来存储/处理收到的消息。
  • @Limey 我更新了问题并将此功能添加到问题的底部。有 # 个市场行,我尝试保存,但没有保存。

标签: r websocket rstudio


【解决方案1】:

我也在解决同样的问题

用闪亮的方法找出解决方案:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多