Class: Anorexic::StubWSCtrl

Inherits:
Object
  • Object
show all
Defined in:
lib/anorexic/handlers/stubs.rb

Overview

a skeleton for a WebSocket controller class which uses REST to emulate long XHR pulling

you dont have to inherit this or use this, this is example/stub code.

WebSockets Controllers and RESTful Controllers can be the same class (the same route can handle both a regular request and a WebSocket request).

if the pre_connect method returns false, the WebSockets connection will be refused and the remaining routes will be attempted.

Instance Method Summary collapse

Constructor Details

#initializeStubWSCtrl

every request that routes to this controller will create a new instance


83
84
# File 'lib/anorexic/handlers/stubs.rb', line 83

def initialize
end

Instance Method Details

#_tell_firends(data) ⇒ Object

a demo event method that recieves a broadcast from instance siblings.

methods that are protected and methods that start with an underscore are hidden from the router BUT, broadcasted methods must be public (or the broadcast will quietly fail)… so we have to use the _underscore for this method.


118
119
120
# File 'lib/anorexic/handlers/stubs.rb', line 118

def _tell_firends data
	response << "Someone said #{data}"			
end

#indexObject

called when request is GET and params isn't defined


127
128
129
# File 'lib/anorexic/handlers/stubs.rb', line 127

def index
	{message: 'index', data: {id: nil, token: nil}}.to_json
end

#on_connectObject

called immediately after a WebSocket connection has been established.


97
98
99
# File 'lib/anorexic/handlers/stubs.rb', line 97

def on_connect
	true
end

#on_disconnectObject

called when a disconnect packet has been recieved or the connection has been cut (ISN'T called after a disconnect message has been sent).


110
111
# File 'lib/anorexic/handlers/stubs.rb', line 110

def on_disconnect
end

#on_message(data) ⇒ Object

called when new data is recieved

data is a string that contains binary or UTF8 (message dependent) data.


104
105
106
# File 'lib/anorexic/handlers/stubs.rb', line 104

def on_message data
	broadcast :_tell_firends, data
end

#pre_connectObject

called before the protocol is swithed from HTTP to WebSockets.

this allows setting headers, cookies and other data (such as authentication) prior to opening a WebSocket.

if the method returns false, the connection will be refused and the remaining routes will be attempted.


92
93
94
# File 'lib/anorexic/handlers/stubs.rb', line 92

def pre_connect
	true
end

#showObject

called when request is GET and params exists (unless params == “new”).


132
133
134
# File 'lib/anorexic/handlers/stubs.rb', line 132

def show
	{message: 'read_chat', data: {id: params[:id], token: cookies['example_token'], example_data: 'we missed you.'}}.to_json
end

#updateObject

called when request is POST / PUT and params exists


136
137
138
139
140
# File 'lib/anorexic/handlers/stubs.rb', line 136

def update
	# assumes body is JSON - more handling could be done using the params (which hold parsed JSON data).
	broadcast :_tell_firends, request[:body] 
	{message: 'write_chat', data: {id: params[:id], token: cookies['example_token'], example_data: 'message sent.'}}.to_json
end