Class: EventMachine::WebSocket::Connection

Inherits:
Connection
  • Object
show all
Includes:
Debugger
Defined in:
lib/em-websocket/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



24
25
26
27
28
29
30
31
32
# File 'lib/em-websocket/connection.rb', line 24

def initialize(options)
  @options = options
  @debug = options[:debug] || false
  @secure = options[:secure] || false
  @tls_options = options[:tls_options] || {}
  @data = ''

  debug [:initialize]
end

Instance Method Details

#close_websocketObject

Use this method to close the websocket connection cleanly This sends a close frame and waits for acknowlegement before closing the connection



37
38
39
40
41
42
43
44
# File 'lib/em-websocket/connection.rb', line 37

def close_websocket
  if @handler
    @handler.close_websocket
  else
    # The handshake hasn't completed - should be safe to terminate
    close_connection
  end
end

#close_with_error(message) ⇒ Object



117
118
119
120
# File 'lib/em-websocket/connection.rb', line 117

def close_with_error(message)
  @onerror.call(message) if @onerror
  close_connection_after_writing
end

#dispatch(data) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/em-websocket/connection.rb', line 66

def dispatch(data)
  if data.match(/\A<policy-file-request\s*\/>/)
    send_flash_cross_domain_file
    return false
  else
    debug [:inbound_headers, data]
    begin
      @data << data
      @handler = HandlerFactory.build(self, @data, @secure, @debug)
      unless @handler
        # The whole header has not been received yet.
        return false
      end
      @data = nil
      @handler.run
      return true
    rescue => e
      debug [:error, e]
      process_bad_request(e)
      return false
    end
  end
end

#onclose(&blk) ⇒ Object



10
# File 'lib/em-websocket/connection.rb', line 10

def onclose(&blk);    @onclose = blk;   end

#onerror(&blk) ⇒ Object



11
# File 'lib/em-websocket/connection.rb', line 11

def onerror(&blk);    @onerror = blk;   end

#onmessage(&blk) ⇒ Object



12
# File 'lib/em-websocket/connection.rb', line 12

def onmessage(&blk);  @onmessage = blk; end

#onopen(&blk) ⇒ Object

define WebSocket callbacks



9
# File 'lib/em-websocket/connection.rb', line 9

def onopen(&blk);     @onopen = blk;    end

#post_initObject



46
47
48
# File 'lib/em-websocket/connection.rb', line 46

def post_init
  start_tls(@tls_options) if @secure
end

#process_bad_request(reason) ⇒ Object



90
91
92
93
94
# File 'lib/em-websocket/connection.rb', line 90

def process_bad_request(reason)
  @onerror.call(reason) if @onerror
  send_data "HTTP/1.1 400 Bad request\r\n\r\n"
  close_connection_after_writing
end

#receive_data(data) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/em-websocket/connection.rb', line 50

def receive_data(data)
  debug [:receive_data, data]

  if @handler
    @handler.receive_data(data)
  else
    dispatch(data)
  end
end

#requestObject



122
123
124
# File 'lib/em-websocket/connection.rb', line 122

def request
  @handler ? @handler.request : {}
end

#send(data) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'lib/em-websocket/connection.rb', line 107

def send(data)
  debug [:send, data]

  if @handler
    @handler.send_text_frame(data)
  else
    raise WebSocketError, "Cannot send data before onopen callback"
  end
end

#send_flash_cross_domain_fileObject



96
97
98
99
100
101
102
103
104
105
# File 'lib/em-websocket/connection.rb', line 96

def send_flash_cross_domain_file
  file =  '<?xml version="1.0"?><cross-domain-policy><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>'
  debug [:cross_domain, file]
  send_data file

  # handle the cross-domain request transparently
  # no need to notify the user about this connection
  @onclose = nil
  close_connection_after_writing
end

#stateObject



126
127
128
# File 'lib/em-websocket/connection.rb', line 126

def state
  @handler ? @handler.state : :handshake
end

#trigger_on_closeObject



20
21
22
# File 'lib/em-websocket/connection.rb', line 20

def trigger_on_close
  @onclose.call if @onclose
end

#trigger_on_message(msg) ⇒ Object



14
15
16
# File 'lib/em-websocket/connection.rb', line 14

def trigger_on_message(msg)
  @onmessage.call(msg) if @onmessage
end

#trigger_on_openObject



17
18
19
# File 'lib/em-websocket/connection.rb', line 17

def trigger_on_open
  @onopen.call if @onopen
end

#unbindObject



60
61
62
63
64
# File 'lib/em-websocket/connection.rb', line 60

def unbind
  debug [:unbind, :connection]

  @handler.unbind if @handler
end