Module: Faye::WebSocket::API

Extended by:
Forwardable
Includes:
EventTarget
Included in:
Faye::WebSocket, Client
Defined in:
lib/faye/websocket/api.rb,
lib/faye/websocket/api/event.rb,
lib/faye/websocket/api/event_target.rb

Defined Under Namespace

Modules: EventTarget Classes: CloseEvent, ErrorEvent, Event, MessageEvent, OpenEvent

Constant Summary collapse

CONNECTING =
0
OPEN =
1
CLOSING =
2
CLOSED =
3
CLOSE_TIMEOUT =
30
TYPES =
{
  'open'    => OpenEvent,
  'message' => MessageEvent,
  'close'   => CloseEvent,
  'error'   => ErrorEvent
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventTarget

#add_event_listener, #add_listener, #dispatch_event, #remove_event_listener

Instance Attribute Details

#buffered_amountObject (readonly)

Returns the value of attribute buffered_amount.



20
21
22
# File 'lib/faye/websocket/api.rb', line 20

def buffered_amount
  @buffered_amount
end

#ready_stateObject (readonly)

Returns the value of attribute ready_state.



20
21
22
# File 'lib/faye/websocket/api.rb', line 20

def ready_state
  @ready_state
end

#urlObject (readonly)

Returns the value of attribute url.



20
21
22
# File 'lib/faye/websocket/api.rb', line 20

def url
  @url
end

Instance Method Details

#close(code = nil, reason = nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/faye/websocket/api.rb', line 79

def close(code = nil, reason = nil)
  code   ||= 1000
  reason ||= ''

  unless code == 1000 or (code >= 3000 and code <= 4999)
    raise ArgumentError, "Failed to execute 'close' on WebSocket: " +
                         "The code must be either 1000, or between 3000 and 4999. " +
                         "#{ code } is neither."
  end

  if @ready_state < CLOSING
    @close_timer = EventMachine.add_timer(CLOSE_TIMEOUT) { begin_close('', 1006) }
  end

  @ready_state = CLOSING unless @ready_state == CLOSED

  @driver.close(reason, code)
end

#initialize(options = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/faye/websocket/api.rb', line 22

def initialize(options = {})
  @ready_state = CONNECTING
  super()
  ::WebSocket::Driver.validate_options(options, [:headers, :extensions, :max_length, :ping, :proxy, :tls])

  @driver = yield

  if headers = options[:headers]
    headers.each { |name, value| @driver.set_header(name, value) }
  end

  [*options[:extensions]].each do |extension|
    @driver.add_extension(extension)
  end

  @ping            = options[:ping]
  @ping_id         = 0
  @buffered_amount = 0

  @close_params = @close_timer = @ping_timer = @proxy = @stream = nil
  @onopen = @onmessage = @onclose = @onerror = nil

  @driver.on(:open)    { |e| open }
  @driver.on(:message) { |e| receive_message(e.data) }
  @driver.on(:close)   { |e| begin_close(e.reason, e.code, :wait_for_write => true) }

  @driver.on(:error) do |error|
    emit_error(error.message)
  end

  if @ping
    @ping_timer = EventMachine.add_periodic_timer(@ping) do
      @ping_id += 1
      ping(@ping_id.to_s)
    end
  end
end

#ping(message = '', &callback) ⇒ Object



74
75
76
77
# File 'lib/faye/websocket/api.rb', line 74

def ping(message = '', &callback)
  return false if @ready_state > OPEN
  @driver.ping(message, &callback)
end

#protocolObject



98
99
100
# File 'lib/faye/websocket/api.rb', line 98

def protocol
  @driver.protocol || ''
end

#send(message) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/faye/websocket/api.rb', line 64

def send(message)
  return false if @ready_state > OPEN
  case message
    when Numeric then @driver.text(message.to_s)
    when String  then @driver.text(message)
    when Array   then @driver.binary(message)
    else false
  end
end

#write(data) ⇒ Object



60
61
62
# File 'lib/faye/websocket/api.rb', line 60

def write(data)
  @stream.write(data)
end