Class: Bidi2pdf::Bidi::Client

Inherits:
Object
  • Object
show all
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.

Examples:

Creating and starting a client

client = Bidi2pdf::Bidi::Client.new("ws://example.com/socket")
client.start

Sending a command

command = Bidi2pdf::Bidi::Commands::ScriptEvaluate.new context: browsing_context_id, expression: script
client.send_cmd(command)

Subscribing to events

client.on_event("eventName") do |event_data|
  puts "Received event: #{event_data}"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws_url) ⇒ Client

Initializes a new WebSocket client.

Parameters:

  • ws_url (String)

    The WebSocket URL to connect to.



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_urlString (readonly)

Returns The WebSocket URL.

Returns:

  • (String)

    The WebSocket URL.



34
35
36
# File 'lib/bidi2pdf/bidi/client.rb', line 34

def ws_url
  @ws_url
end

Instance Method Details

#closeObject

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.

Yields:

  • A block to execute when the 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.

Yields:

  • (error)

    A block to handle the error.



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.

Parameters:

  • names (Array<String>)

    The names of the events to subscribe to.

Yields:

  • (event_data)

    A block to handle the event data.



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.

Yields:

  • (message)

    A block to handle the incoming message.



107
# File 'lib/bidi2pdf/bidi/client.rb', line 107

def on_message(&) = dispatcher.on_message(&)

#on_open { ... } ⇒ Object

Registers a callback for when the WebSocket connection is opened.

Yields:

  • A block to execute when the 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.

Parameters:

  • names (Array<String>)

    The names of the events to unsubscribe from.

  • block (Proc)

    The listener block to remove.



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.

Parameters:

  • block (Proc)

    The listener block to remove.



139
# File 'lib/bidi2pdf/bidi/client.rb', line 139

def remove_message_listener(listener) = dispatcher.remove_message_listener(listener)

#send_cmd(cmd) ⇒ Object

Sends a command to the WebSocket server.

Parameters:

Raises:



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.

Parameters:

  • cmd (Object)

    The command to send.

  • timeout (Integer) (defaults to: Bidi2pdf.default_timeout)

    The timeout duration in seconds.

Yields:

  • (response)

    A block to handle the response.

Raises:



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

#startWebSocket::Client::Simple

Starts the WebSocket client and establishes a connection.

Returns:

  • (WebSocket::Client::Simple)

    The WebSocket connection object.



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.on_message { |data| handle_response_to_cmd(data) }
    dispatcher.start_listening
  end

  @started = true

  @socket
end

#started?Boolean

Checks if the WebSocket client has started.

Returns:

  • (Boolean)

    True if the client has started, false otherwise.



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.

Parameters:

  • timeout (Integer) (defaults to: Bidi2pdf.default_timeout)

    The timeout duration in seconds.

Raises:



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