Class: Celluloid::EventSource

Inherits:
Object
  • Object
show all
Includes:
IO
Defined in:
lib/celluloid/eventsource.rb,
lib/celluloid/eventsource/version.rb,
lib/celluloid/eventsource/response_parser.rb

Defined Under Namespace

Classes: MessageEvent, ResponseParser

Constant Summary collapse

CONNECTING =
0
OPEN =
1
CLOSED =
2
VERSION =
"0.3.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, options = {}) {|_self| ... } ⇒ EventSource

Returns a new instance of EventSource.

Yields:

  • (_self)

Yield Parameters:


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/celluloid/eventsource.rb', line 19

def initialize(uri, options = {})
  self.url = uri
  options  = options.dup
  @ready_state = CONNECTING
  @with_credentials = options.delete(:with_credentials) { false }
  @headers = default_request_headers.merge(options.fetch(:headers, {}))

  @event_type_buffer = ""
  @last_event_id_buffer = ""
  @data_buffer = ""

  @last_event_id = String.new

  @reconnect_timeout = 10
  @on = { open: ->{}, message: ->(_) {}, error: ->(_) {} }
  @parser = ResponseParser.new

  @chunked = false

  yield self if block_given?

  async.listen
end

Instance Attribute Details

#ready_stateObject (readonly)

Returns the value of attribute ready_state


11
12
13
# File 'lib/celluloid/eventsource.rb', line 11

def ready_state
  @ready_state
end

#urlObject

Returns the value of attribute url


10
11
12
# File 'lib/celluloid/eventsource.rb', line 10

def url
  @url
end

#with_credentialsObject (readonly)

Returns the value of attribute with_credentials


10
11
12
# File 'lib/celluloid/eventsource.rb', line 10

def with_credentials
  @with_credentials
end

Instance Method Details

#closeObject


64
65
66
67
# File 'lib/celluloid/eventsource.rb', line 64

def close
  @socket.close if @socket
  @ready_state = CLOSED
end

#closed?Boolean

Returns:

  • (Boolean)

51
52
53
# File 'lib/celluloid/eventsource.rb', line 51

def closed?
  ready_state == CLOSED
end

#connected?Boolean

Returns:

  • (Boolean)

47
48
49
# File 'lib/celluloid/eventsource.rb', line 47

def connected?
  ready_state == OPEN
end

#listenObject


55
56
57
58
59
60
61
62
# File 'lib/celluloid/eventsource.rb', line 55

def listen
  establish_connection

  chunked? ? process_chunked_stream : process_stream
rescue IOError
  # Closing the socket during read causes this exception and kills the actor
  # We really don't wan to do anything if the socket is closed.
end

#on(event_name, &action) ⇒ Object


69
70
71
# File 'lib/celluloid/eventsource.rb', line 69

def on(event_name, &action)
  @on[event_name.to_sym] = action
end

#on_error(&action) ⇒ Object


81
82
83
# File 'lib/celluloid/eventsource.rb', line 81

def on_error(&action)
  @on[:error] = action
end

#on_message(&action) ⇒ Object


77
78
79
# File 'lib/celluloid/eventsource.rb', line 77

def on_message(&action)
  @on[:message] = action
end

#on_open(&action) ⇒ Object


73
74
75
# File 'lib/celluloid/eventsource.rb', line 73

def on_open(&action)
  @on[:open] = action
end