Class: Ruflet::Server
- Inherits:
-
Object
- Object
- Ruflet::Server
- Defined in:
- lib/ruflet/server.rb
Constant Summary collapse
- WEBSOCKET_GUID =
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"- TASK_IO_POLL_INTERVAL =
0.01
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #bind_server_socket!(max_attempts: 100) ⇒ Object
-
#handle_upgraded_socket(io) ⇒ Object
For Rack-hosted mode: caller already performed the HTTP upgrade.
-
#initialize(host: "0.0.0.0", port: 8550, in_process_bridge: nil, &app_block) ⇒ Server
constructor
A new instance of Server.
- #publish_runtime_port ⇒ Object
- #reload_app! ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(host: "0.0.0.0", port: 8550, in_process_bridge: nil, &app_block) ⇒ Server
Returns a new instance of Server.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/ruflet/server.rb', line 19 def initialize(host: "0.0.0.0", port: 8550, in_process_bridge: nil, &app_block) @host = host @port = port @in_process_bridge = in_process_bridge @app_block = app_block @sessions = {} @sessions_mutex = Mutex.new @connections = {} @connections_mutex = Mutex.new @running = false @server_socket = nil at_exit do begin stop rescue StandardError nil end end end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
14 15 16 |
# File 'lib/ruflet/server.rb', line 14 def port @port end |
Instance Method Details
#bind_server_socket!(max_attempts: 100) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruflet/server.rb', line 70 def bind_server_socket!(max_attempts: 100) requested = @port.to_i candidate = requested attempts = ENV["RUFLET_STRICT_PORT"] == "1" ? 1 : max_attempts attempts.times do begin @server_socket = TCPServer.new(@host, candidate) @port = @server_socket.addr[1] publish_runtime_port if requested > 0 && @port != requested && ENV["RUFLET_SUPPRESS_SERVER_BANNER"] != "1" warn "Requested port #{requested} is busy; bound to #{@port}" end return rescue Errno::EADDRINUSE candidate += 1 end end raise Errno::EADDRINUSE, "Unable to bind port #{requested}" if attempts == 1 raise Errno::EADDRINUSE, "Unable to bind starting at #{requested} after #{max_attempts} attempts" end |
#handle_upgraded_socket(io) ⇒ Object
For Rack-hosted mode: caller already performed the HTTP upgrade.
65 66 67 68 |
# File 'lib/ruflet/server.rb', line 65 def handle_upgraded_socket(io) ws = Ruflet::WebSocketConnection.new(io) run_connection(ws) end |
#publish_runtime_port ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/ruflet/server.rb', line 117 def publish_runtime_port path = ENV["RUFLET_RUNTIME_PORT_FILE"].to_s return if path.empty? File.open(path, "w") { |file| file.write(@port.to_s) } rescue StandardError => error warn "Unable to publish Ruflet runtime port: #{error.}" end |
#reload_app! ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/ruflet/server.rb', line 126 def reload_app! snapshots = @sessions_mutex.synchronize { @sessions.to_a } snapshots.each do |session_key, current_page| ws = @connections_mutex.synchronize { @connections[session_key] } next unless ws # Open dialogs, sheets, and snack bars are Navigator routes on the # client; replacing the control tree does not pop them. Close them # first, otherwise a reload while an overlay is open leaves it on # screen wired to control ids the reloaded page cannot resolve. begin nil while current_page.respond_to?(:close_dialog) && current_page.close_dialog rescue StandardError nil end if current_page.respond_to?(:reset_for_reload!) # Re-render on the live page. The client's overlay/service/dialogs # containers stay mounted; a recreated Page would re-send them, # replacing their client-side instances and detaching them — after # which dialog and service patches are silently ignored. current_page.reset_for_reload! @app_block.call(current_page) # Clear page-level chrome the reloaded block dropped and flush the # overlay (appbar/drawer/FAB live in the view and rebuild wholesale; # page props and the mounted overlay need explicit updates). current_page.finalize_reload! if current_page.respond_to?(:finalize_reload!) # Keeps route-driven apps on their current route: replays the # route_change event when the block did not route itself. current_page.replay_route_after_reload! if current_page.respond_to?(:replay_route_after_reload!) current_page.update else # Older ruflet_core without reset_for_reload!: fall back to a fresh # page (loses client-side container bindings until reconnect). refreshed_page = Page.new( session_id: current_page.session_id, client_details: current_page.client_details, sender: lambda do |action, payload| (ws, action, payload) end ) refreshed_page.title = "Ruflet App" @sessions_mutex.synchronize do @sessions[session_key] = refreshed_page end @app_block.call(refreshed_page) refreshed_page.update end rescue StandardError => e warn "reload error: #{e.class}: #{e.}" end end |
#start ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ruflet/server.rb', line 40 def start if Object.const_defined?(:Task) && Task.current.name == "main" server = self task = Task.new(name: "ruflet-server") { server.start } Task.run return task end previous_signals = trap_stop_signals if in_process_transport_requested? start_in_process_transport else bind_server_socket! @running = true accept_loop end rescue Interrupt nil ensure stop restore_stop_signals(previous_signals) end |
#stop ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ruflet/server.rb', line 94 def stop return unless @running || @server_socket @running = false server = @server_socket @server_socket = nil begin server&.close rescue IOError nil end live_connections = @connections_mutex.synchronize { @connections.values.dup } live_connections.each do |conn| begin conn.close rescue StandardError nil end end end |