rstudio / httpuv

HTTP and WebSocket server package for R
Other
227 stars 86 forks source link

no response in linux #399

Closed jbShi1017 closed 5 months ago

jbShi1017 commented 5 months ago

`library(httpuv)

s <- startServer(host = "0.0.0.0", port = 8080, app = list( call = function(req) { body <- paste0("Time: ", Sys.time(), "
Path requested: ", req$PATH_INFO) list( status = 200L, headers = list('Content-Type' = 'text/html'), body = body ) } ) )

while (TRUE){

}`

I used this code to create a service on Linux, but there was no response when accessing it, and there was no problem with Windows httpuv version: 1.6.15 R version: 4.3.3

wch commented 5 months ago

Are you trying to connect from localhost, or from another computer? If you are able to connect from localhost but not another computer, then I think it is likely that your Linux machine has some firewall rules that are blocking the connection.

jbShi1017 commented 5 months ago

unadble to connect from localhost image

wch commented 5 months ago

Oh, I think I see the problem. It's the while(TRUE){} loop. This blocks the R process and won't get the httpuv server a chance to respond to anything. I am surprised to hear that this same code works on Windows.

You should get rid of the while loop and use runServer() instead of startServer(). The runServer() function will start the server and then drive the event loop which httpuv uses to respond to requests.

Or you could call startServer() and then call httpuv::service(0). That is essentially what runServer() does.

jbShi1017 commented 5 months ago

Oh,thank you!