Module: Rack::WebSocket::Extensions::Common

Defined in:
lib/rack/websocket/extensions/common.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#websocketObject

Returns the value of attribute websocket.



19
20
21
# File 'lib/rack/websocket/extensions/common.rb', line 19

def websocket
  @websocket
end

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rack/websocket/extensions/common.rb', line 6

def self.included(base)
  base.class_eval do
    alias :receive_data_without_websocket :receive_data
    alias :receive_data :receive_data_with_websocket

    alias :unbind_without_websocket :unbind
    alias :unbind :unbind_with_websocket

    alias :receive_data_without_flash_policy_file :receive_data
    alias :receive_data :receive_data_with_flash_policy_file
  end
end

Instance Method Details

#receive_data_with_flash_policy_file(data) ⇒ Object

Send flash policy file if requested



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rack/websocket/extensions/common.rb', line 47

def receive_data_with_flash_policy_file(data)
  # thin require data to be proper http request - in it's not
  # then @request.parse raises exception and data isn't parsed
  # by futher methods. Here we only check if it is flash
  # policy file request ("<policy-file-request/>\000") and
  # if so then flash policy file is returned. if not then
  # rest of request is handled.
  if (data == "<policy-file-request/>\000")
    file =  '<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>'
    # ignore errors - we will close this anyway
    send_data(file) rescue nil
    close_connection_after_writing
  else
    receive_data_without_flash_policy_file(data)
  end
end

#receive_data_with_websocket(data) ⇒ Object

Skip default receive_data if this is WebSocket connection



28
29
30
31
32
33
34
# File 'lib/rack/websocket/extensions/common.rb', line 28

def receive_data_with_websocket(data)
  if self.websocket?
    self.websocket.receive_data(data)
  else
    receive_data_without_websocket(data)
  end
end

#unbind_with_websocketObject

Skip standard unbind it this is WebSocket connection



38
39
40
41
42
43
44
# File 'lib/rack/websocket/extensions/common.rb', line 38

def unbind_with_websocket
  if self.websocket?
    self.websocket.unbind
  else
    unbind_without_websocket
  end
end

#websocket?Boolean

Is this connection WebSocket?

Returns:

  • (Boolean)


22
23
24
# File 'lib/rack/websocket/extensions/common.rb', line 22

def websocket?
  !self.websocket.nil?
end