Class: Iodine::Http::WebsocketHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/iodine/http/websocket_handler.rb

Overview

This class is a good demonstration for creating a Websocket handler with the Iodine API.

Iodine is Object Oriented and for this reason the Websocket handler is expected to retain the information it needs - either through initialization, or through the ‘on_open(protocol)` callback.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, response) ⇒ WebsocketHandler

this is called while still communicating over Http (during the upgrade process).



15
16
17
18
# File 'lib/iodine/http/websocket_handler.rb', line 15

def initialize request, response
	@request = request
	@response = response
end

Instance Attribute Details

#requestObject (readonly)

The original Http request



11
12
13
# File 'lib/iodine/http/websocket_handler.rb', line 11

def request
  @request
end

#responseObject (readonly)

The Http response, also allowing for websocket data



13
14
15
# File 'lib/iodine/http/websocket_handler.rb', line 13

def response
  @response
end

Class Method Details

.call(request, response) ⇒ Object

This method allows the class itself to act as the Websocket handler, usable with:

Iodine::Http.on_websocket Iodine::Http::WebsocketEchoDemo


34
35
36
# File 'lib/iodine/http/websocket_handler.rb', line 34

def self.call request, response
	self.new request, response
end

Instance Method Details

#on_broadcast(data) ⇒ Object

Accept unicasts or broadcasts using this callback.



26
27
# File 'lib/iodine/http/websocket_handler.rb', line 26

def on_broadcast data
end

#on_closeObject

cleanup, if needed, using this callback.



29
30
# File 'lib/iodine/http/websocket_handler.rb', line 29

def on_close
end

#on_message(data) ⇒ Object

Accept data using this callback - this is a required callback.



23
24
# File 'lib/iodine/http/websocket_handler.rb', line 23

def on_message data
end

#on_openObject

initialize the protocol data once the connection had opened.



20
21
# File 'lib/iodine/http/websocket_handler.rb', line 20

def on_open
end