class Jekyll::Livereload::HttpAwareConnection
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.
Attributes
reload_file[R]
Public Class Methods
new(opts)
click to toggle source
Calls superclass method
# File lib/jekyll-livereload/websocket.rb, line 39 def initialize(opts) @reload_file = File.join(LIVERELOAD_DIR, "livereload.js") super opts end
Public Instance Methods
dispatch(data)
click to toggle source
Calls superclass method
# 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