Class: Guard::LiveReload::WebSocket

Inherits:
EventMachine::WebSocket::Connection
  • Object
show all
Defined in:
lib/guard/livereload/websocket.rb

Constant Summary collapse

HTTP_DATA_FORBIDDEN =
"HTTP/1.1 403 Forbidden\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n403 Forbidden"
HTTP_DATA_NOT_FOUND =
"HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n404 Not Found"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ WebSocket

Returns a new instance of WebSocket.



12
13
14
15
# File 'lib/guard/livereload/websocket.rb', line 12

def initialize(options)
  @livereload_js_path = options[:livereload_js_path]
  super
end

Instance Method Details

#dispatch(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/guard/livereload/websocket.rb', line 17

def dispatch(data)
  parser = Http::Parser.new
  parser << data
  # prepend with '.' to make request url usable as a file path
  request_path = '.' + URI.parse(parser.request_url).path
  request_path += '/index.html' if File.directory? request_path
  if parser.http_method != 'GET' || parser.upgrade?
    super # pass the request to websocket
  else
    _serve(request_path)
  end
end