Class: RailsLiveReload::Server::Base
- Inherits:
-
Object
- Object
- RailsLiveReload::Server::Base
- Includes:
- Connections
- Defined in:
- lib/rails_live_reload/server/base.rb
Overview
This class is based on ActionCable github.com/rails/rails/blob/v7.0.3/actioncable/lib/action_cable/server/base.rb
Constant Summary
Constants included from Connections
Instance Attribute Summary collapse
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
Instance Method Summary collapse
-
#call(env) ⇒ Object
Called by Rack to set up the server.
- #client_javascript ⇒ Object
- #event_loop ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #reload_all ⇒ Object
- #setup_socket ⇒ Object
Methods included from Connections
#add_connection, #connections, #remove_connection, #setup_heartbeat_timer
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
23 24 25 26 |
# File 'lib/rails_live_reload/server/base.rb', line 23 def initialize @mutex = Monitor.new @event_loop = nil end |
Instance Attribute Details
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
15 16 17 |
# File 'lib/rails_live_reload/server/base.rb', line 15 def mutex @mutex end |
Instance Method Details
#call(env) ⇒ Object
Called by Rack to set up the server.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rails_live_reload/server/base.rb', line 29 def call(env) case env["REQUEST_PATH"] when RailsLiveReload.config.url setup_socket setup_heartbeat_timer request = Rack::Request.new(env) RailsLiveReload::WebSocket::Base.new(self, request).process when "#{RailsLiveReload.config.url}/script" content = client_javascript [200, {'Content-Type' => 'application/javascript', 'Content-Length' => content.size.to_s, 'Cache-Control' => 'no-store'}, [content]] else raise ActionController::RoutingError, 'Not found' end end |
#client_javascript ⇒ Object
44 45 46 |
# File 'lib/rails_live_reload/server/base.rb', line 44 def client_javascript @client_javascript || @mutex.synchronize { @client_javascript ||= File.open(File.join(File.dirname(__FILE__), "../javascript/websocket.js")).read } end |
#event_loop ⇒ Object
48 49 50 |
# File 'lib/rails_live_reload/server/base.rb', line 48 def event_loop @event_loop || @mutex.synchronize { @event_loop ||= RailsLiveReload::WebSocket::EventLoop.new } end |
#reload_all ⇒ Object
17 18 19 20 21 |
# File 'lib/rails_live_reload/server/base.rb', line 17 def reload_all @mutex.synchronize do connections.each(&:reload) end end |
#setup_socket ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/rails_live_reload/server/base.rb', line 52 def setup_socket @socket ||= UNIXSocket.open(RailsLiveReload.config.socket_path).tap do |socket| Thread.new do loop do data = JSON.parse socket.readline case data["event"] when RailsLiveReload::INTERNAL[:socket_events][:reload] RailsLiveReload::Checker.files = data['files'] reload_all else raise NotImplementedError end end end end end |