Class: Jekyll::Livereload::HttpAwareConnection
- Inherits:
-
EventMachine::WebSocket::Connection
- Object
- EventMachine::WebSocket::Connection
- Jekyll::Livereload::HttpAwareConnection
- Defined in:
- lib/jekyll-livereload/websocket.rb
Overview
The LiveReload protocol requires the server to serve livereload.js over HTTP despite the fact that the protocol itself uses WebSockets. This custom connection class addresses the dual protocols that the server needs to understand.
Instance Attribute Summary collapse
-
#reload_file ⇒ Object
readonly
Returns the value of attribute reload_file.
Instance Method Summary collapse
- #dispatch(data) ⇒ Object
-
#initialize(opts) ⇒ HttpAwareConnection
constructor
A new instance of HttpAwareConnection.
Constructor Details
#initialize(opts) ⇒ HttpAwareConnection
Returns a new instance of HttpAwareConnection.
39 40 41 42 |
# File 'lib/jekyll-livereload/websocket.rb', line 39 def initialize(opts) super @reload_file = File.join(LIVERELOAD_DIR, "livereload.js") end |
Instance Attribute Details
#reload_file ⇒ Object (readonly)
Returns the value of attribute reload_file.
37 38 39 |
# File 'lib/jekyll-livereload/websocket.rb', line 37 def reload_file @reload_file end |
Instance Method Details
#dispatch(data) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/jekyll-livereload/websocket.rb', line 44 def dispatch(data) parser = Http::Parser.new parser << data # WebSockets requests will have a Connection: Upgrade header if parser.http_method != 'GET' || parser.upgrade? super elsif parser.request_url =~ /^\/livereload.js/ headers = [ 'HTTP/1.1 200 OK', 'Content-Type: application/javascript', "Content-Length: #{File.size(reload_file)}", '', '', ].join("\r\n") send_data(headers) stream_file_data(reload_file).callback do close_connection_after_writing end else body = "This port only serves livereload.js over HTTP.\n" headers = [ 'HTTP/1.1 400 Bad Request', 'Content-Type: text/plain', "Content-Length: #{body.bytesize}", '', '', ].join("\r\n") send_data(headers) send_data(body) close_connection_after_writing end end |