@@ -29,6 +29,10 @@ open class OpenAIRealtimeSession {
29
29
self . receiveMessage ( )
30
30
}
31
31
32
+ deinit {
33
+ logIf ( . debug) ? . debug ( " OpenAIRealtimeSession is being freed " )
34
+ }
35
+
32
36
/// Messages sent from OpenAI are published on this receiver as they arrive
33
37
public var receiver : AsyncStream < OpenAIRealtimeMessage > {
34
38
return AsyncStream { continuation in
@@ -39,7 +43,7 @@ open class OpenAIRealtimeSession {
39
43
/// Sends a message through the websocket connection
40
44
public func sendMessage( _ encodable: Encodable ) async {
41
45
guard !self . isTearingDown else {
42
- logIf ( . warning ) ? . warning ( " Can't send a websocket message, the RT session is tearing down." )
46
+ logIf ( . debug ) ? . debug ( " Ignoring ws sendMessage. The RT session is tearing down." )
43
47
return
44
48
}
45
49
do {
@@ -52,6 +56,7 @@ open class OpenAIRealtimeSession {
52
56
53
57
/// Close the websocket connection
54
58
public func disconnect( ) {
59
+ logIf ( . debug) ? . debug ( " Disconnecting from realtime session " )
55
60
self . isTearingDown = true
56
61
self . continuation? . finish ( )
57
62
self . continuation = nil
@@ -76,7 +81,10 @@ open class OpenAIRealtimeSession {
76
81
77
82
/// Handles socket errors. We disconnect on all errors.
78
83
private func didReceiveWebSocketError( _ error: NSError ) {
79
- if ( error. code == 57 ) {
84
+ guard !isTearingDown else {
85
+ return
86
+ }
87
+ if error. code == 57 {
80
88
logIf ( . warning) ? . warning ( " WS disconnected. Check that your AIProxy project is websocket enabled and you've followed the DeviceCheck integration guide " )
81
89
} else {
82
90
logIf ( . error) ? . error ( " Received ws error: \( error. localizedDescription) " )
0 commit comments