Module: Faye::WebSocket::API

Includes:
Publisher
Included in:
Faye::WebSocket, Client
Defined in:
lib/faye/util/web_socket/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Publisher

#bind, #count_listeners, #trigger, #unbind

Instance Attribute Details

#buffered_amountObject (readonly)

Returns the value of attribute buffered_amount.



10
11
12
# File 'lib/faye/util/web_socket/api.rb', line 10

def buffered_amount
  @buffered_amount
end

#oncloseObject

Returns the value of attribute onclose.



11
12
13
# File 'lib/faye/util/web_socket/api.rb', line 11

def onclose
  @onclose
end

#onerrorObject

Returns the value of attribute onerror.



11
12
13
# File 'lib/faye/util/web_socket/api.rb', line 11

def onerror
  @onerror
end

#onmessageObject

Returns the value of attribute onmessage.



11
12
13
# File 'lib/faye/util/web_socket/api.rb', line 11

def onmessage
  @onmessage
end

#onopenObject

Returns the value of attribute onopen.



11
12
13
# File 'lib/faye/util/web_socket/api.rb', line 11

def onopen
  @onopen
end

#ready_stateObject (readonly)

Returns the value of attribute ready_state.



10
11
12
# File 'lib/faye/util/web_socket/api.rb', line 10

def ready_state
  @ready_state
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/faye/util/web_socket/api.rb', line 10

def url
  @url
end

Instance Method Details

#add_event_listener(type, listener, use_capture) ⇒ Object



55
56
57
# File 'lib/faye/util/web_socket/api.rb', line 55

def add_event_listener(type, listener, use_capture)
  bind(type, listener)
end

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/faye/util/web_socket/api.rb', line 30

def close(code = nil, reason = nil, ack = true)
  return if [CLOSING, CLOSED].include?(ready_state)
  
  @ready_state = CLOSING
  
  close = lambda do
    @ready_state = CLOSED
    @stream.close_connection_after_writing
    event = Event.new('close', :code => code || 1000, :reason => reason || '')
    event.init_event('close', false, false)
    dispatch_event(event)
  end
  
  if ack
    if @parser.respond_to?(:close)
      @parser.close(code, reason, &close)
    else
      close.call
    end
  else
    @parser.close(code, reason) if @parser.respond_to?(:close)
    close.call
  end
end

#dispatch_event(event) ⇒ Object



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

def dispatch_event(event)
  event.target = event.current_target = self
  event.event_phase = Event::AT_TARGET
  
  trigger(event.type, event)
  callback = __send__("on#{ event.type }")
  callback.call(event) if callback
end

#receive(data) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/faye/util/web_socket/api.rb', line 15

def receive(data)
  return false unless ready_state == OPEN
  event = Event.new('message')
  event.init_event('message', false, false)
  event.data = data
  dispatch_event(event)
end

#remove_event_listener(type, listener, use_capture) ⇒ Object



59
60
61
# File 'lib/faye/util/web_socket/api.rb', line 59

def remove_event_listener(type, listener, use_capture)
  unbind(type, listener)
end

#send(data, type = nil, error_type = nil) ⇒ Object



23
24
25
26
27
28
# File 'lib/faye/util/web_socket/api.rb', line 23

def send(data, type = nil, error_type = nil)
  return false if ready_state == CLOSED
  data = Faye.encode(data) if String === data
  frame = @parser.frame(data, type, error_type)
  @stream.write(frame) if frame
end