17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/markedly/app.rb', line 17
def run!
@document.convert
puts "Open '%s'" % @document.uri
Launchy.open(@document.uri)
EM.run do
@channel = EM::Channel.new
debug "Start WebSocket Server (ws://localhost:#{@document.port})"
EM::WebSocket.start(host: '0.0.0.0', port: @document.port) do |ws|
ws.onopen do
subscriber = @channel.subscribe do |message|
ws.send(message)
end
debug "client #{subscriber} connected"
ws.onclose do
@channel.unsubscribe(subscriber)
debug "client #{subscriber} closed"
end
end
end
EM.defer do
mtime = last_mtime = File.mtime(@document.source)
loop do
mtime = File.mtime(@document.source)
if mtime > last_mtime
puts "file modified"
@document.convert
@channel.push 'updated'
end
last_mtime = mtime
sleep @options[:interval]
end
end
end
end
|