Class: Faye::WebSocket

Inherits:
Object
  • Object
show all
Includes:
Publisher
Defined in:
lib/faye/util/web_socket.rb

Defined Under Namespace

Classes: Event, Stream

Constant Summary collapse

CONNECTING =
0
OPEN =
1
CLOSING =
2
CLOSED =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Publisher

#add_subscriber, #count_subscribers, #publish_event, #remove_subscriber, #remove_subscribers

Constructor Details

#initialize(request) ⇒ WebSocket

Returns a new instance of WebSocket.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/faye/util/web_socket.rb', line 14

def initialize(request)
  @request  = request
  @callback = @request.env['async.callback']
  @stream   = Stream.new
  @callback.call [200, RackAdapter::TYPE_JSON, @stream]
  
  @url = @request.env['websocket.url']
  @ready_state = OPEN
  @buffered_amount = 0
  
  event = Event.new
  event.init_event('open', false, false)
  dispatch_event(event)
  
  @buffer = []
  @buffering = false
  
  @request.env[Thin::Request::WEBSOCKET_RECEIVE_CALLBACK] = lambda do |data|
    data.each_char(&method(:handle_char))
  end
end

Instance Attribute Details

#buffered_amountObject (readonly)

Returns the value of attribute buffered_amount.



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

def buffered_amount
  @buffered_amount
end

#oncloseObject

Returns the value of attribute onclose.



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

def onclose
  @onclose
end

#onerrorObject

Returns the value of attribute onerror.



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

def onerror
  @onerror
end

#onmessageObject

Returns the value of attribute onmessage.



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

def onmessage
  @onmessage
end

#onopenObject

Returns the value of attribute onopen.



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

def onopen
  @onopen
end

#ready_stateObject (readonly)

Returns the value of attribute ready_state.



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

def ready_state
  @ready_state
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#add_event_listener(type, listener, use_capture) ⇒ Object



44
45
46
# File 'lib/faye/util/web_socket.rb', line 44

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

#closeObject



41
42
# File 'lib/faye/util/web_socket.rb', line 41

def close
end

#dispatch_event(event) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/faye/util/web_socket.rb', line 52

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

#remove_event_listener(type, listener, use_capture) ⇒ Object



48
49
50
# File 'lib/faye/util/web_socket.rb', line 48

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

#send(data) ⇒ Object



36
37
38
39
# File 'lib/faye/util/web_socket.rb', line 36

def send(data)
  string = ["\x00", data, "\xFF"].map(&method(:encode)) * ''
  @stream.write(string)
end