Skip to content
This repository has been archived by the owner on Dec 7, 2018. It is now read-only.

Subclassing Reel::Server

//de edited this page Dec 12, 2013 · 3 revisions

Reel lets you define your own web server actors by building on top of Reel::Server:

require 'reel'

class MyServer < Reel::Server::HTTP
  def initialize(host = "127.0.0.1", port = 3000)
    super(host, port, &method(:on_connection))
  end

  def on_connection(connection)
    connection.each_request do |request|
      request.respond :ok, "Hello, world!"
    end
  end
end

MyServer.run

Using a subclass of Reel::Server::HTTP works a lot like the Basic Usage, except instead of passing a block to the #initialize method, we can define a connection handler method, and pass that a Method object to the connection handler method we want to use, in this case on_connection.

This is nice because blocks are closures and can reference objects in the outer scope. The blocks you pass to Reel::Server::HTTP#initialize reference an outer scope which is likely shared with other threads. This means if you end up accessing objects in the outer scope from both a Reel::Server::HTTP and another thread, you could run into concurrent mutation bugs.

This form lets you make the connection handler a method, eliminating any chance that objects in the outer scope could end up shared across threads because there isn't an outer scope to be shared.

Clone this wiki locally