class OpalHotReloader::Server
Most of this lifted from github.com/saward/Rubame
Constants
- PROGRAM
Attributes
directories[R]
Public Class Methods
new(options)
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 13 def initialize(options) Socket.do_not_reverse_lookup @hostname = '0.0.0.0' @port = options[:port] setup_directories(options) @reading = [] @writing = [] @clients = {} # Socket as key, and Client as value @socket = TCPServer.new(@hostname, @port) @reading.push @socket end
Public Instance Methods
accept()
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 45 def accept socket = @socket.accept_nonblock @reading.push socket handshake = WebSocket::Handshake::Server.new client = Client.new(socket, handshake, self) while line = socket.gets client.handshake << line break if client.handshake.finished? end if client.handshake.valid? @clients[socket] = client client.write handshake.to_s client.opened = true return client else close(client) end return nil end
close(client)
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 146 def close(client) @reading.delete client.socket @clients.delete client.socket begin client.socket.close rescue end client.closed = true end
loop()
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 95 def loop listener = Listen.to(*@directories, only: %r{\.(rb(\.erb)?|s?[ac]ss)$}) do |modified, added, removed| modified.each { |modified_file| send_updated_file(modified_file) } puts "modified absolute path: #{modified}" puts "added absolute path: #{added}" puts "removed absolute path: #{removed}" end listener.start puts "#{PROGRAM}: starting..." while (!$quit) run do |client| client.onopen do puts "#{PROGRAM}: client open" end client.onmessage do |mess| puts "PROGRAM: message received: #{mess}" unless mess == '' end client.onclose do puts "#{PROGRAM}: client closed" end end sleep 0.2 end end
read(client)
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 121 def read(client) pairs = client.socket.recvfrom(2000) messages = [] if pairs[0].length == 0 close(client) else client.frame << pairs[0] while f = client.frame.next if (f.type == :close) close(client) return messages else messages.push f end end end return messages end
run(time = 0, &blk)
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 156 def run(time = 0, &blk) readable, writable = IO.select(@reading, @writing, nil, 0) if readable readable.each do |socket| client = @clients[socket] if socket == @socket client = accept else msg = read(client) client.messaged = msg end blk.call(client) if client and blk end end # Check for lazy send items timer_start = Time.now time_passed = 0 begin @clients.each do |s, c| c.send_some_lazy(5) end time_passed = Time.now - timer_start end while time_passed < time end
send_updated_file(modified_file)
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 67 def send_updated_file(modified_file) if modified_file =~ /\.rb(\.erb)?$/ file_contents = File.read(modified_file).force_encoding(Encoding::UTF_8) update = { type: 'ruby', filename: modified_file, source_code: file_contents }.to_json end if modified_file =~ /\.s?[ac]ss$/ # TODO: Switch from hard-wired path assumptions to using SASS/sprockets config relative_path = Pathname.new(modified_file).relative_path_from(Pathname.new(Dir.pwd)) url = relative_path.to_s .sub('public/','') .sub('/sass/','/') .sub(/\.s[ac]ss/, '.css') update = { type: 'css', filename: modified_file, url: url }.to_json end if update @clients.each { |socket, client| client.send(update) } end end
setup_directories(options)
click to toggle source
adds known directories automatically if they exist
-
rails js app/assets/javascripts app/assets/stylesheets
-
reactrb rails defaults app/views/components
-
you tell me and I'll add them
# File lib/opal_hot_reloader/server.rb, line 32 def setup_directories(options) @directories = options[:directories] || [] [ 'app/assets/javascripts', 'app/assets/stylesheets', 'app/views/components' ].each { |known_dir| if !@directories.include?(known_dir) && File.exists?(known_dir) @directories << known_dir end } end
stop()
click to toggle source
# File lib/opal_hot_reloader/server.rb, line 184 def stop @socket.close end