Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What option do I have for embedded HTTP/HTTPS server for Ruby app?

I am making some game server. As like any other game server does, I think the server should be state-ful. (might be change later, but currently I am looking for state-ful solution)

After playing with Rake some, I decided to find a solution in Ruby. What I am finding is:

  • An embeddable HTTP server library which can be integrated long-running Ruby app. (in-process lib)
  • Should support handling of bare-bone HTTP request/response handling. No decoration, URL dispatching or web-page template. (which I never need)
  • Should offer hard single-threaded mode.
  • Should support HTTPS with self-signed certificate.
  • Reliable, and proven in production.
  • Nice documentation and large community.

I think most similar example is HTTP server integrated into node.js. Basically a thin layer on top of TCP socket.

It doesn't need to support multi-threading. I think I will run separated process for each CPU core and I need fast development, so multithreading is currently what to evade.

I have looked for Rack, and it seems like just a protocol specification, and is not an actual implementation. And it looks like for state-less web-app only. If it is not,please correct me.

So, what kind of options are available for these stuffs in Ruby?

like image 393
eonil Avatar asked Nov 20 '25 12:11

eonil


1 Answers

After careful re-evaluation of your question I might have found another solution.

Perhaps the only thing you need is a simple TCP socket server. A primitive HTTP 1.0 server can be done with the Ruby core classes like this:

require 'socket'

server = TCPServer.open(80)
loop do
  client = server.accept

  response = "<html>...</html>"
  headers = ["HTTP/1.0 200 OK",
             "Content-Type: text/html",
             "Content-Length: #{response.length}"].join("\r\n")

  client.puts headers
  client.puts "\r\n\r\n"
  client.puts response

  client.close
end

Have a look at these resources:

  • Ruby documentation TCPServer
  • Ruby Socket Programming
like image 178
Daniel Rikowski Avatar answered Nov 24 '25 08:11

Daniel Rikowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!