Class: Bidi2pdf::Bidi::Client
- Inherits:
-
Object
- Object
- Bidi2pdf::Bidi::Client
- Defined in:
- lib/bidi2pdf/bidi/client.rb
Overview
Represents a WebSocket client for managing communication with a remote server using the Bidi2pdf library. This class handles the setup, connection, and communication with the WebSocket server, including sending commands and handling responses.
Instance Attribute Summary collapse
-
#ws_url ⇒ String
readonly
The WebSocket URL.
Instance Method Summary collapse
-
#close ⇒ Object
Closes the WebSocket connection.
-
#initialize(ws_url) ⇒ Client
constructor
Initializes a new WebSocket client.
-
#on_close { ... } ⇒ Object
Registers a callback for when the WebSocket connection is closed.
-
#on_error {|error| ... } ⇒ Object
Registers a callback for WebSocket errors.
-
#on_event(*names) {|event_data| ... } ⇒ Object
Subscribes to specific WebSocket events.
-
#on_message {|message| ... } ⇒ Object
Registers a callback for incoming WebSocket messages.
-
#on_open { ... } ⇒ Object
Registers a callback for when the WebSocket connection is opened.
-
#remove_event_listener(*names, listener) ⇒ Object
Removes event listeners for specific events.
-
#remove_message_listener(listener) ⇒ Object
Removes a message listener.
-
#send_cmd(cmd) ⇒ Object
Sends a command to the WebSocket server.
-
#send_cmd_and_wait(cmd, timeout: Bidi2pdf.default_timeout) {|response| ... } ⇒ Object
Sends a command to the WebSocket server and waits for a response.
-
#start ⇒ WebSocket::Client::Simple
Starts the WebSocket client and establishes a connection.
-
#started? ⇒ Boolean
Checks if the WebSocket client has started.
-
#wait_until_open(timeout: Bidi2pdf.default_timeout) ⇒ Object
Waits until the WebSocket connection is open.
Constructor Details
#initialize(ws_url) ⇒ Client
Initializes a new WebSocket client.
39 40 41 42 43 |
# File 'lib/bidi2pdf/bidi/client.rb', line 39 def initialize(ws_url) @ws_url = ws_url @started = false @connection_manager = ConnectionManager.new(logger: Bidi2pdf.logger) end |
Instance Attribute Details
#ws_url ⇒ String (readonly)
Returns The WebSocket URL.
34 35 36 |
# File 'lib/bidi2pdf/bidi/client.rb', line 34 def ws_url @ws_url end |
Instance Method Details
#close ⇒ Object
Closes the WebSocket connection.
150 151 152 153 154 155 156 157 |
# File 'lib/bidi2pdf/bidi/client.rb', line 150 def close return unless @socket Bidi2pdf.logger.debug "Closing WebSocket connection" @socket&.close @socket = nil @started = false end |
#on_close { ... } ⇒ Object
Registers a callback for when the WebSocket connection is closed.
117 |
# File 'lib/bidi2pdf/bidi/client.rb', line 117 def on_close(&) = dispatcher.on_close(&) |
#on_error {|error| ... } ⇒ Object
Registers a callback for WebSocket errors.
122 |
# File 'lib/bidi2pdf/bidi/client.rb', line 122 def on_error(&) = dispatcher.on_error(&) |
#on_event(*names) {|event_data| ... } ⇒ Object
Subscribes to specific WebSocket events.
128 129 130 131 132 133 134 |
# File 'lib/bidi2pdf/bidi/client.rb', line 128 def on_event(*names, &) listener = dispatcher.on_event(*names, &) cmd = Bidi2pdf::Bidi::Commands::SessionSubscribe.new(events: names) send_cmd(cmd) if names.any? listener end |
#on_message {|message| ... } ⇒ Object
Registers a callback for incoming WebSocket messages.
107 |
# File 'lib/bidi2pdf/bidi/client.rb', line 107 def (&) = dispatcher.(&) |
#on_open { ... } ⇒ Object
Registers a callback for when the WebSocket connection is opened.
112 |
# File 'lib/bidi2pdf/bidi/client.rb', line 112 def on_open(&) = dispatcher.on_open(&) |
#remove_event_listener(*names, listener) ⇒ Object
Removes event listeners for specific events.
145 146 147 |
# File 'lib/bidi2pdf/bidi/client.rb', line 145 def remove_event_listener(*names, listener) names.each { |event_name| dispatcher.remove_event_listener(event_name, listener) } end |
#remove_message_listener(listener) ⇒ Object
Removes a message listener.
139 |
# File 'lib/bidi2pdf/bidi/client.rb', line 139 def (listener) = dispatcher.(listener) |
#send_cmd(cmd) ⇒ Object
Sends a command to the WebSocket server.
86 87 88 89 90 |
# File 'lib/bidi2pdf/bidi/client.rb', line 86 def send_cmd(cmd) raise Bidi2pdf::ClientError, "Client#start must be called before" unless started? @command_manager.send_cmd(cmd) end |
#send_cmd_and_wait(cmd, timeout: Bidi2pdf.default_timeout) {|response| ... } ⇒ Object
Sends a command to the WebSocket server and waits for a response.
98 99 100 101 102 |
# File 'lib/bidi2pdf/bidi/client.rb', line 98 def send_cmd_and_wait(cmd, timeout: Bidi2pdf.default_timeout, &) raise Bidi2pdf::ClientError, "Client#start must be called before" unless started? @command_manager.send_cmd_and_wait(cmd, timeout: timeout, &) end |
#start ⇒ WebSocket::Client::Simple
Starts the WebSocket client and establishes a connection.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/bidi2pdf/bidi/client.rb', line 48 def start return @socket if started? WebSocket::Client::Simple.connect(ws_url) do |socket| @socket = socket @command_manager = CommandManager.new(@socket) dispatcher.on_open { @connection_manager.mark_connected } dispatcher. { |data| handle_response_to_cmd(data) } dispatcher.start_listening end @started = true @socket end |
#started? ⇒ Boolean
Checks if the WebSocket client has started.
68 |
# File 'lib/bidi2pdf/bidi/client.rb', line 68 def started? = @started |
#wait_until_open(timeout: Bidi2pdf.default_timeout) ⇒ Object
Waits until the WebSocket connection is open.
74 75 76 77 78 79 80 |
# File 'lib/bidi2pdf/bidi/client.rb', line 74 def wait_until_open(timeout: Bidi2pdf.default_timeout) @connection_manager.wait_until_open(timeout: timeout) rescue Bidi2pdf::WebsocketError => e raise Bidi2pdf::WebsocketError, "Client#start must be called within #{timeout} sec." unless started? raise e end |