class RDoc::Server
A minimal HTTP server for live-reloading RDoc documentation.
Uses Ruby’s built-in TCPServer (no external dependencies).
Used by rdoc --server to let developers preview documentation while editing source files. Parses sources once on startup, watches for file changes, re-parses only the changed files, and auto-refreshes the browser via a simple polling script.
Constants
- CONTENT_TYPES
- STATUS_TEXTS
Public Class Methods
Source
# File lib/rdoc/server.rb, line 26 def self.live_reload_script(last_change_time) <<~JS <script> (function() { var lastChange = #{last_change_time}; setInterval(function() { fetch('/__status').then(function(r) { return r.json(); }).then(function(data) { if (data.last_change > lastChange) location.reload(); lastChange = data.last_change; }).catch(function() {}); }, 1000); })(); </script> JS end
Returns a live-reload polling script with the given last_change_time embedded so the browser knows the exact timestamp of the content it received. This avoids a race where a change that occurs between page generation and the first poll would be silently skipped.
Source
# File lib/rdoc/server.rb, line 64 def initialize(rdoc, port) @rdoc = rdoc @options = rdoc.options @store = rdoc.store @port = port @generator = create_generator @page_cache = {} @search_index_cache = nil @last_change_time = Time.now.to_f @mutex = Mutex.new @running = false end
Creates a new server.
rdoc is the RDoc::RDoc instance that has already parsed the source files. port is the TCP port to listen on.
Public Instance Methods
Source
# File lib/rdoc/server.rb, line 102 def shutdown @running = false @tcp_server&.close @watcher_thread&.join(2) end
Shuts down the server.
Source
# File lib/rdoc/server.rb, line 81 def start @tcp_server = TCPServer.new('127.0.0.1', @port) @running = true @watcher_thread = start_watcher(@rdoc.last_modified.keys) url = "http://localhost:#{@port}" $stderr.puts "\nServing documentation at: \e]8;;#{url}\e\\#{url}\e]8;;\e\\" $stderr.puts "Press Ctrl+C to stop.\n\n" loop do client = @tcp_server.accept Thread.new(client) { |c| handle_client(c) } rescue IOError break end end
Starts the server. Blocks until interrupted.