Module: Iodine::Http

Extended by:
Http
Included in:
Http
Defined in:
lib/iodine/http.rb,
lib/iodine/http/hpack.rb,
lib/iodine/http/http1.rb,
lib/iodine/http/http2.rb,
lib/iodine/http/request.rb,
lib/iodine/http/session.rb,
lib/iodine/http/response.rb,
lib/iodine/http/websockets.rb,
lib/iodine/http/rack_support.rb,
lib/iodine/http/websocket_client.rb,
lib/iodine/http/websocket_handler.rb

Overview

The Http class allows the creation of Http and Websocket servers using Iodine.

To start an Http server, simply require ‘iodine/http` (which isn’t required by default) and set up your Http callback. i.e.:

require 'iodine/http'
Iodine::Http.on_http { |request, response| 'Hello World!' }
exit # only if running from irb

To start a Websocket server, require ‘iodine/http` (which isn’t required by default), create a Websocket handling Class and set up your Websocket callback. i.e.:

require 'iodine/http'
class WSChatServer
   def initialize nickname, response
       @nickname = nickname || "unknown"
       @response = response
       # @response.io # => Http Protocol
   end
   def on_open
       # only now is the response.io pointing at the Websocket Protocol
       @io = @response.io
       @io.broadcast "#{@nickname} has joined the chat!"
       @io << "Welcome #{@nickname}, you have joined the chat!"
   end
   def on_message data
       @io.broadcast "#{@nickname} >> #{data}"
       @io << ">> #{data}"
   end
   def on_broadcast data
       # the http response can also be used to send websocket data.
       @response << data
   end
   def on_close
       @io.broadcast "#{@nickname} has left the chat!"
   end
end

Iodine::Http.on_websocket { |request, response| WSChatServer.new request.params[:name], response}

See WebsocketHandler for a good starting point or inherit WebsocketHandler in your handler.

Defined Under Namespace

Modules: Rack, SessionManager Classes: Http1, Http2, Request, Response, WebsocketClient, WebsocketHandler, Websockets

Instance Method Summary collapse

Instance Method Details

#http2Object

Returns true if Iodine will require that new connection be encrypted.



117
118
119
# File 'lib/iodine/http.rb', line 117

def http2
	@http2
end

#http2=(allow) ⇒ Object

Sets whether Iodine will allow connections to the experiemntal Http2 protocol. Defaults to false unless the ‘http2` command line flag is present.



113
114
115
# File 'lib/iodine/http.rb', line 113

def http2= allow
	@http2 = allow && true
end

#max_http_bufferObject

Gets the maximum bytes allowed for an HTTP’s body request (file upload data). Defaults to the command line ‘-limit` argument, or ~0.25GB.



108
109
110
# File 'lib/iodine/http.rb', line 108

def max_http_buffer
	@max_http_buffer
end

#max_http_buffer=(size) ⇒ Object

Sets the maximum bytes allowed for an HTTP’s body request (file upload data). Defaults to the command line ‘-limit` argument, or ~0.25GB.



104
105
106
# File 'lib/iodine/http.rb', line 104

def max_http_buffer= size
	@max_http_buffer = size
end

#on_http(handler = nil, &block) ⇒ Object

Sets or gets the Http callback.

An Http callback is a Proc like object that answers to ‘call(request, response)` and returns either:

‘true`

the response has been set by the callback and can be managed (including any streaming) by the server.

‘false`

the request shouldn’t be answered or resource not found (error 404 will be sent as a response).

String

the String will be appended to the response and the response sent.



80
81
82
83
# File 'lib/iodine/http.rb', line 80

def on_http handler = nil, &block
	@http_app = handler || block if handler || block
	@http_app
end

#on_websocket(handler = nil, &block) ⇒ Object

Sets or gets the Websockets callback.

A Websockets callback is a Proc like object that answers to ‘call(request)` and returns either:

‘false`

the request shouldn’t be answered or resource not found (error 404 will be sent as a response).

Websocket Handler

a Websocket handler is an object that is expected to answer ‘on_message(data)` and `on_close`. See {} for more data.



89
90
91
92
# File 'lib/iodine/http.rb', line 89

def on_websocket handler = nil, &block
	@websocket_app = handler || block if handler || block
	@websocket_app
end

#session_tokenObject

Gets the session token for the Http server (String). Defaults to the name of the script.



99
100
101
# File 'lib/iodine/http.rb', line 99

def session_token
	@session_token
end

#session_token=(token) ⇒ Object

Sets the session token for the Http server (String). Defaults to the name of the script + ‘_id’.



95
96
97
# File 'lib/iodine/http.rb', line 95

def session_token= token
	@session_token = token
end

#ws_connect(url, options = {}, &block) ⇒ Object

Creates a websocket client within a new task (non-blocking).

Make sure to setup all the callbacks (as needed) prior to starting the connection. See Iodine::Http::WebsocketClient.connect

i.e.:

require 'iodine/http'
# don't start the server
Iodine.protocol = :timer
options = {}
options[:on_open] = Proc.new { write "Hello there!"}
options[:on_message] = Proc.new do |data|
    puts ">> #{data}";
    write "Bye!";
    # It's possible to update the callback midstream.
    on_message {|data| puts "-- Goodbye message: #{data}"; close;}
end
# After closing we will call `Iodine.signal_exit` to signal Iodine to finish up.
options[:on_close] = Proc.new { puts "disconnected"; Iodine.signal_exit }

Iodine::Http.ws_connect "ws://echo.websocket.org", options

#if running from irb:
exit


155
156
157
# File 'lib/iodine/http.rb', line 155

def ws_connect url, options={}, &block
	::Iodine.run { ::Iodine::Http::WebsocketClient.connect url, options, &block }
end