2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/eventmachine/websocket/server.rb', line 2
def self.start(host, port, &block)
EventMachine::WebSocket.run(host: host, port: port, block: block) do |ws|
ws.onerror do |e|
if EventMachine::WebSocket::WebSocketError === e
log "[!] websocket error.", error: e
else
fail e
end
end
fail "inactivity timeout" unless ws.comm_inactivity_timeout.zero?
def ws.receive_data(data)
return unless data
method, path, _httpv = data.split(" ", 3)
path, _qs = path.split("?", 2)
send_error = lambda do |status|
send_data("HTTP/1.1 #{status}\r\n\r\n#{status}")
close_connection_after_writing
log "[!] http client error.", method: method, path: path, status: status
end
if method != "GET"
send_error.call(405)
else
@options[:block].call(path, self) || send_error.call(404)
end
singleton_class.remove_method(__method__)
singleton_class.remove_method(:serve_file)
super if defined? @onopen
end
def ws.serve_file(path)
send_data("HTTP/1.1 200 OK\r\n\r\n")
send_data(path.read)
close_connection_after_writing
end
end
end
|