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
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.



18
19
20
# File 'lib/faye/websocket/api.rb', line 18

def buffered_amount
  @buffered_amount
end

#ready_stateObject (readonly)

Returns the value of attribute ready_state.



18
19
20
# File 'lib/faye/websocket/api.rb', line 18

def ready_state
  @ready_state
end

#urlObject (readonly)

Returns the value of attribute url.



18
19
20
# File 'lib/faye/websocket/api.rb', line 18

def url
  @url
end

Instance Method Details

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



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/faye/websocket/api.rb', line 77

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

  @ready_state = CLOSING unless @ready_state == CLOSED
  @driver.close(reason, code)
end

#initialize(options = {}) ⇒ Object



20
21
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
# File 'lib/faye/websocket/api.rb', line 20

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 = @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) }

  @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



72
73
74
75
# File 'lib/faye/websocket/api.rb', line 72

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

#protocolObject



91
92
93
# File 'lib/faye/websocket/api.rb', line 91

def protocol
  @driver.protocol || ''
end

#send(message) ⇒ Object



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

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



58
59
60
# File 'lib/faye/websocket/api.rb', line 58

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