Module: SinatraWebsocket::Ext::Thin::Connection

Defined in:
lib/sinatra-websocket/ext/thin/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#websocketObject

Returns the value of attribute websocket


21
22
23
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 21

def websocket
  @websocket
end

Class Method Details

.included(base) ⇒ Object


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 5

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

    alias :pre_process_without_websocket :pre_process
    alias :pre_process :pre_process_with_websocket
  end
end

Instance Method Details

#pre_process_with_websocketObject

Set 'async.connection' Rack env


24
25
26
27
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 24

def pre_process_with_websocket
  @request.env['async.connection'] = self
  pre_process_without_websocket
end

#receive_data_with_flash_policy_file(data) ⇒ Object

Send flash policy file if requested


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 52

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


36
37
38
39
40
41
42
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 36

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


46
47
48
49
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 46

def unbind_with_websocket
  self.websocket? && self.websocket.unbind
  unbind_without_websocket
end

#websocket?Boolean

Is this connection WebSocket?

Returns:

  • (Boolean)

30
31
32
# File 'lib/sinatra-websocket/ext/thin/connection.rb', line 30

def websocket?
  !self.websocket.nil?
end