Class: Agoo::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/agoo/base.rb

Overview

Maybe not the best class name but this class implements the base methds for a Rack WebSocket/SSE handler. A handler does not have to inherit from this class but the class does identify the methods supported.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
# File 'lib/agoo/base.rb', line 10

def initialize(env)
  # Nothing needs to be done on initialize although the #call env is
  # available if desired for some reason on creaton. After #on_open is
  # called the same env can be accessed through the client.
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



8
9
10
# File 'lib/agoo/base.rb', line 8

def connection
  @connection
end

Instance Method Details

#closeObject

Closes the WebSocket or SSE connection. Note the connection can also be closed by the JavaScript listener.



52
53
54
55
# File 'lib/agoo/base.rb', line 52

def close
  check_connection
  connection.close
end

#on_close(client) ⇒ Object

Called when the WebSocket or SSE connection is closed.



25
26
27
# File 'lib/agoo/base.rb', line 25

def on_close(client)
  @connection = nil
end

#on_drained(client) ⇒ Object

This indicates the send delivery queue is empty. Useful if attempting to restrict the sends to one at a time.



31
32
# File 'lib/agoo/base.rb', line 31

def on_drained(client)
end

#on_error(client, err_msg) ⇒ Object

Called when an error occurs on the WebSocket or SSE connection.



40
41
42
# File 'lib/agoo/base.rb', line 40

def on_error(client, err_msg)
  # Handle an error.
end

#on_message(client, data) ⇒ Object

Called when the JavaScript listener sends a WebSocket message.



35
36
37
# File 'lib/agoo/base.rb', line 35

def on_message(client, data)
  # Handle a received message.
end

#on_open(client) ⇒ Object

Called when the WebSocket or SSE connection has been established. The client represents the connection and cab be used to send messages to the JavaScript WebSocket or SSE listener.



19
20
21
22
# File 'lib/agoo/base.rb', line 19

def on_open(client)
  # save the client to support a send later on.
  @connection = client
end

#publish(subject, message) ⇒ Object



72
73
74
75
# File 'lib/agoo/base.rb', line 72

def publish(subject, message)
  check_connection
  connection.publish(subject, message)
end

#send_message(msg) ⇒ Object

Sends a message to the WebSocket or SSE listener.



45
46
47
48
# File 'lib/agoo/base.rb', line 45

def send_message(msg)
  check_connection
  connection.write(msg)
end

#subscribe(subject) ⇒ Object

The #subscribe, #unsubscribe, and #publish methods are not part of the Rack SPEC but add added here to demonstrate the publish and subscribe add-on that Agoo and Iodine provide. If you think it is something that should be included in the Rack SPEC we can add that. It was left out as it made the API more complex and



62
63
64
65
# File 'lib/agoo/base.rb', line 62

def subscribe(subject)
  check_connection
  connection.subscribe(subject)
end

#unsubscribe(subject = nil) ⇒ Object



67
68
69
70
# File 'lib/agoo/base.rb', line 67

def unsubscribe(subject=nil)
  check_connection
  connection.unsubscribe(subject)
end