Class: Faye::WebSocket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
API
Defined in:
lib/faye/util/web_socket.rb,
lib/faye/util/web_socket/api.rb,
lib/faye/util/web_socket/client.rb,
lib/faye/util/web_socket/draft75_parser.rb,
lib/faye/util/web_socket/draft76_parser.rb,
lib/faye/util/web_socket/protocol8_parser.rb

Defined Under Namespace

Modules: API Classes: Client, Draft75Parser, Draft76Parser, Event, Protocol8Parser, Stream

Constant Summary collapse

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

Instance Attribute Summary collapse

Attributes included from API

#buffered_amount, #onclose, #onerror, #onmessage, #onopen, #ready_state, #url

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

#add_event_listener, #close, #dispatch_event, #receive, #remove_event_listener, #send

Methods included from Publisher

#bind, #count_listeners, #trigger, #unbind

Constructor Details

#initialize(env) ⇒ WebSocket

Returns a new instance of WebSocket.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/faye/util/web_socket.rb', line 28

def initialize(env)
  @env      = env
  @callback = @env['async.callback']
  @stream   = Stream.new(self, @env['em.connection'])
  @callback.call [200, RackAdapter::TYPE_JSON, @stream]
  
  @url = determine_url
  @ready_state = CONNECTING
  @buffered_amount = 0
  
  @parser = WebSocket.parser(@env).new(self)
  @stream.write(@parser.handshake_response)
  
  @ready_state = OPEN
  
  event = Event.new('open')
  event.init_event('open', false, false)
  dispatch_event(event)
  
  @env[Thin::Request::WEBSOCKET_RECEIVE_CALLBACK] = @parser.method(:parse)
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

Class Method Details

.parser(env) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/faye/util/web_socket.rb', line 18

def self.parser(env)
  if env['HTTP_SEC_WEBSOCKET_VERSION']
    Protocol8Parser
  elsif env['HTTP_SEC_WEBSOCKET_KEY1']
    Draft76Parser
  else
    Draft75Parser
  end
end