Class: OpenAI::WebSocket::Connection Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/openai/helpers/websocket/connection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Product-neutral socket lifecycle and text I/O.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket:, url:) ⇒ Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Connection.



66
67
68
69
70
# File 'lib/openai/helpers/websocket/connection.rb', line 66

def initialize(socket:, url:)
  @socket = socket
  @url = url
  @poisoned = false
end

Instance Attribute Details

#url ⇒ URI::Generic (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (URI::Generic)


63
64
65
# File 'lib/openai/helpers/websocket/connection.rb', line 63

def url
  @url
end

Instance Method Details

#abort ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Abort without waiting for the WebSocket close handshake.



129
130
131
132
133
134
# File 'lib/openai/helpers/websocket/connection.rb', line 129

def abort
  return if socket_closed?

  @socket.abort
  nil
end

#close(code: 1000, reason: "") ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Close the connection.



118
119
120
121
122
123
124
# File 'lib/openai/helpers/websocket/connection.rb', line 118

def close(code: 1000, reason: "")
  return abort if @poisoned
  return if socket_closed?

  @socket.close(code: code, reason: reason)
  nil
end

#closed? ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



137
# File 'lib/openai/helpers/websocket/connection.rb', line 137

def closed? = @poisoned || socket_closed?

#each ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Yield server events until the remote peer closes the connection.



73
74
75
76
77
78
79
80
81
# File 'lib/openai/helpers/websocket/connection.rb', line 73

def each
  return enum_for(__method__) unless block_given?

  while (event = receive)
    yield(event)
  end

  self
end

#receive ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Receive and parse the next server event, or return nil after a clean close.



84
85
86
87
88
89
# File 'lib/openai/helpers/websocket/connection.rb', line 84

def receive
  data = receive_raw
  return nil if data.nil?

  parse_event(data)
end

#receive_raw ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Receive the next raw WebSocket message.



94
# File 'lib/openai/helpers/websocket/connection.rb', line 94

def receive_raw = read_raw_message

#send_raw(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Send an already encoded text message.

Raises:

  • (connection_error(closed_send_message))


106
107
108
109
110
111
112
113
114
115
# File 'lib/openai/helpers/websocket/connection.rb', line 106

def send_raw(data)
  raise connection_error(closed_send_message), cause: nil if closed?

  text = data.dup
  text.force_encoding(Encoding::UTF_8) if text.encoding == Encoding::BINARY
  text = text.encode(Encoding::UTF_8) unless text.encoding == Encoding::UTF_8
  raise ArgumentError, invalid_text_message unless text.valid_encoding?

  write_text(text)
end