Websocket code only works in interactive session
br00t-coursera opened this issue · 1 comments
br00t-coursera commented
I don't know if this is readily fixable, but I thought I would ask...
- Create a script called
websocketscript.R
with the following contents:
#!/usr/bin/Rscript
library(websocket)
ws <- WebSocket$new("ws://echo.websocket.org/", autoConnect = FALSE)
ws$onOpen(function(event) {
cat("Connection opened\n")
})
ws$onMessage(function(event) {
cat("Client got msg: ", event$data, "\n")
})
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()
Sys.sleep(1)
ws$send('hello')
ws$close()
- make the script executable ...
chmod a+x websocketscript.R
- run the script in the shell ...
./websocketscript.R
Is there any way for this to work as it would in an interactive session? I gather the problem is related to the fact that the process terminates before the callbacks ever execute? I have tried such hacky foolishness in the shell as:
R < websocketscript.R --interactive
printf source('websocketscript.R') | Rscript --interactive websocketscript.R
No joy. Thoughts?
> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.4 LTS
Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
locale:
[1] LC_CTYPE=en_CA.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_CA.UTF-8 LC_COLLATE=en_CA.UTF-8
[5] LC_MONETARY=en_CA.UTF-8 LC_MESSAGES=en_CA.UTF-8
[7] LC_PAPER=en_CA.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] websocket_1.1.0.9001 compiler_3.6.3 processx_3.4.2
[4] R6_2.4.1 Matrix_1.3-0 RcppRedis_0.1.10
[7] later_1.0.0.9002 Rcpp_1.0.4 reticulate_1.15
[10] RApiSerialize_0.1.0 codetools_0.2-16 grid_3.6.3
[13] jsonlite_1.6.1 ps_1.3.2 lattice_0.20-41
Thanks!
br00t-coursera commented
Duplicate of #52