Class: Faye::WebSocket

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Publisher
Defined in:
lib/faye/util/web_socket.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

Classes: Draft75Parser, Draft76Parser, Event, Protocol8Parser, Stream

Constant Summary

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

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Publisher

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

Constructor Details

- (WebSocket) initialize(request)

A new instance of WebSocket



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

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
  
  event = Event.new
  event.init_event('open', false, false)
  dispatch_event(event)
  
  @parser = WebSocket.parser(@request).new(self)
  @request.env[Thin::Request::WEBSOCKET_RECEIVE_CALLBACK] = @parser.method(:parse)
end

Instance Attribute Details

- (Object) onclose

Returns the value of attribute onclose



16
17
18
# File 'lib/faye/util/web_socket.rb', line 16

def onclose
  @onclose
end

- (Object) onerror

Returns the value of attribute onerror



16
17
18
# File 'lib/faye/util/web_socket.rb', line 16

def onerror
  @onerror
end

- (Object) onmessage

Returns the value of attribute onmessage



16
17
18
# File 'lib/faye/util/web_socket.rb', line 16

def onmessage
  @onmessage
end

- (Object) onopen

Returns the value of attribute onopen



16
17
18
# File 'lib/faye/util/web_socket.rb', line 16

def onopen
  @onopen
end

- (Object) ready_state (readonly)

Returns the value of attribute ready_state



15
16
17
# File 'lib/faye/util/web_socket.rb', line 15

def ready_state
  @ready_state
end

- (Object) url (readonly)

Returns the value of attribute url



15
16
17
# File 'lib/faye/util/web_socket.rb', line 15

def url
  @url
end

Class Method Details

+ (Object) parser(request)



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

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

Instance Method Details

- (Object) add_event_listener(type, listener, use_capture)



63
64
65
# File 'lib/faye/util/web_socket.rb', line 63

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

- (Object) close



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

def close
end

- (Object) dispatch_event(event)



71
72
73
74
75
76
77
78
# File 'lib/faye/util/web_socket.rb', line 71

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

- (Object) encode(string, encoding = 'UTF-8')



80
81
82
83
# File 'lib/faye/util/web_socket.rb', line 80

def encode(string, encoding = 'UTF-8')
  return string unless string.respond_to?(:force_encoding)
  string.force_encoding(encoding)
end

- (Object) receive(data)



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

def receive(data)
  event = Event.new
  event.init_event('message', false, false)
  event.data = encode(data)
  dispatch_event(event)
end

- (Object) remove_event_listener(type, listener, use_capture)



67
68
69
# File 'lib/faye/util/web_socket.rb', line 67

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

- (Object) send(data, type = nil, error_type = nil)



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

def send(data, type = nil, error_type = nil)
  frame = @parser.frame(encode(data), type, error_type)
  @stream.write(frame) if frame
end