Class: LivereloadRails::Client
- Inherits:
-
Object
- Object
- LivereloadRails::Client
- Defined in:
- lib/livereload_rails/client.rb
Constant Summary collapse
- FSM =
{ initial: {}, opened: { "hello" => :on_hello }, idle: { "info" => nil }, closed: {}, }
Instance Method Summary collapse
- #alert(message) ⇒ Object
- #close(reason = nil) ⇒ Object
-
#initialize(ws) ⇒ Client
constructor
A new instance of Client.
- #on_hello(frame) ⇒ Object
- #reload(path, live: true) ⇒ Object
Constructor Details
#initialize(ws) ⇒ Client
Returns a new instance of Client.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/livereload_rails/client.rb', line 10 def initialize(ws) @state = :initial @connection = ws @connection.on(:open) { @state = :opened } @connection.on(:close) { close } @connection.on(:message) do |frame| data = JSON.parse(frame.data) command = data["command"] if FSM[@state].has_key?(command) if handler = FSM[@state][command] public_send(handler, data) end else raise "Unexpected #{data["command"].inspect} in #{@state}." end end end |
Instance Method Details
#alert(message) ⇒ Object
46 47 48 |
# File 'lib/livereload_rails/client.rb', line 46 def alert() send_data(command: "alert", message: ) end |
#close(reason = nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/livereload_rails/client.rb', line 50 def close(reason = nil) if @state != :closed @state = :closed alert(reason) if reason @connection.close true else false end end |
#on_hello(frame) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/livereload_rails/client.rb', line 30 def on_hello(frame) send_data({ command: "hello", protocols: [ "http://livereload.com/protocols/official-7" ], serverName: "Elabs' LivereloadRails", }) @state = :idle end |
#reload(path, live: true) ⇒ Object
42 43 44 |
# File 'lib/livereload_rails/client.rb', line 42 def reload(path, live: true) send_data(command: "reload", path: path, liveCSS: live) end |