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

Commit

Permalink
Subclass form for defining servers
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Dec 28, 2012
1 parent a18f68d commit fed8d09
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
53 changes: 46 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ and are not amortized across all concurrent requests.
Usage
-----

### Rack support
### Framework Adapters
#### Rack

Reel can be used as a standard Rack server via the "reel" command line
application. Please be aware that Rack support is experimental and that there
Expand All @@ -73,7 +74,7 @@ they must be written to disk.
To really leverage Reel's capabilities, you must use Reel via its own API,
or another Ruby library with direct Reel support.

### Webmachine adapter
#### Webmachine

The most notable library with native Reel support is
[webmachine-ruby](https://github.com/seancribbs/webmachine-ruby),
Expand Down Expand Up @@ -107,10 +108,14 @@ MyApp.run

See the Webmachine documentation for further information

### "Bare metal" Ruby API
### Ruby API

Reel also provides a "bare metal" API which was used in the benchmarks above.
Here's an example of using it:
Here are some examples:

#### Block Form

Reel lets you pass a block to initialize which receives connections:

```ruby
require 'reel'
Expand All @@ -120,11 +125,11 @@ Reel::Server.supervise("0.0.0.0", 3000) do |connection|
case request
when Reel::Request
puts "Client requested: #{request.method} #{request.url}"
connection.respond :ok, "hello, world"
request.respond :ok, "Hello, world!"
when Reel::WebSocket
puts "Client made a WebSocket request to: #{request.url}"
request << "Hello there"
connection.close
request << "Hello everyone out there in WebSocket land"
request.close
break
end
end
Expand All @@ -135,6 +140,40 @@ When we read a request from the incoming connection, we'll either get back
a Reel::Request object, indicating a normal HTTP connection, or a
Reel::WebSocket object for WebSockets connections.

### Subclass Form

You can also subclass Reel, which allows additional customizations:

```ruby
require 'reel'

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

def on_connection(connection)
while request = connection.request
case request
when Reel::Request
handle_request(request)
when Reel::WebSocket
handle_websocket(request)
end
end
end

def handle_request(request)
request.respond :ok, "Hello, world!"
end

def handle_websocket(sock)
sock << "Hello everyone out there in WebSocket land!"
sock.close
end
end
```

Contributing
------------

Expand Down
5 changes: 5 additions & 0 deletions lib/reel/request.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
require 'forwardable'

module Reel
class Request
extend Forwardable
include RequestMixin

UPGRADE = 'Upgrade'.freeze
Expand Down Expand Up @@ -28,6 +31,8 @@ def self.read(connection)
end
end

def_delegators :@connection, :respond, :finish_response, :close

def initialize(http_parser, connection = nil)
@http_parser, @connection = http_parser, connection
end
Expand Down

0 comments on commit fed8d09

Please sign in to comment.