Class: Faye::WebSocket
- Inherits:
-
Object
- Object
- Faye::WebSocket
- Includes:
- Publisher
- Defined in:
- lib/faye/util/web_socket.rb
Defined Under Namespace
Constant Summary collapse
- CONNECTING =
0
- OPEN =
1
- CLOSING =
2
- CLOSED =
3
Instance Attribute Summary collapse
-
#buffered_amount ⇒ Object
readonly
Returns the value of attribute buffered_amount.
-
#onclose ⇒ Object
Returns the value of attribute onclose.
-
#onerror ⇒ Object
Returns the value of attribute onerror.
-
#onmessage ⇒ Object
Returns the value of attribute onmessage.
-
#onopen ⇒ Object
Returns the value of attribute onopen.
-
#ready_state ⇒ Object
readonly
Returns the value of attribute ready_state.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #add_event_listener(type, listener, use_capture) ⇒ Object
- #close ⇒ Object
- #dispatch_event(event) ⇒ Object
-
#initialize(request) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #remove_event_listener(type, listener, use_capture) ⇒ Object
- #send(data) ⇒ Object
Methods included from Publisher
#add_subscriber, #count_subscribers, #publish_event, #remove_subscriber
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_amount ⇒ Object (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 |
#onclose ⇒ Object
Returns the value of attribute onclose.
12 13 14 |
# File 'lib/faye/util/web_socket.rb', line 12 def onclose @onclose end |
#onerror ⇒ Object
Returns the value of attribute onerror.
12 13 14 |
# File 'lib/faye/util/web_socket.rb', line 12 def onerror @onerror end |
#onmessage ⇒ Object
Returns the value of attribute onmessage.
12 13 14 |
# File 'lib/faye/util/web_socket.rb', line 12 def @onmessage end |
#onopen ⇒ Object
Returns the value of attribute onopen.
12 13 14 |
# File 'lib/faye/util/web_socket.rb', line 12 def onopen @onopen end |
#ready_state ⇒ Object (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 |
#url ⇒ Object (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 |
#close ⇒ Object
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 |