@@ -32,6 +32,7 @@ import com.google.firebase.ai.type.Tool
32
32
import com.google.firebase.annotations.concurrent.Blocking
33
33
import com.google.firebase.appcheck.interop.InteropAppCheckTokenProvider
34
34
import com.google.firebase.auth.internal.InternalAuthProvider
35
+ import io.ktor.client.plugins.websocket.DefaultClientWebSocketSession
35
36
import io.ktor.websocket.Frame
36
37
import io.ktor.websocket.close
37
38
import io.ktor.websocket.readBytes
@@ -114,31 +115,28 @@ internal constructor(
114
115
)
115
116
.toInternal()
116
117
val data: String = Json .encodeToString(clientMessage)
118
+ var webSession: DefaultClientWebSocketSession ? = null
117
119
try {
118
- val webSession = controller.getWebSocketSession(location)
119
- try {
120
- webSession.send(Frame .Text (data))
121
- val receivedJsonStr = webSession.incoming.receive().readBytes().toString(Charsets .UTF_8 )
122
- val receivedJson = JSON .parseToJsonElement(receivedJsonStr)
120
+ webSession = controller.getWebSocketSession(location)
121
+ webSession.send(Frame .Text (data))
122
+ val receivedJsonStr = webSession.incoming.receive().readBytes().toString(Charsets .UTF_8 )
123
+ val receivedJson = JSON .parseToJsonElement(receivedJsonStr)
123
124
124
- return if (receivedJson is JsonObject && " setupComplete" in receivedJson) {
125
- LiveSession (
126
- session = webSession,
127
- blockingDispatcher = blockingDispatcher,
128
- firebaseApp = firebaseApp
129
- )
130
- } else {
131
- webSession.close()
132
- throw ServiceConnectionHandshakeFailedException (" Unable to connect to the server" )
133
- }
134
- } catch (e: ClosedReceiveChannelException ) {
135
- val reason = webSession.closeReason.await()
136
- val message =
137
- " Channel was closed by the server.${if (reason!= null ) " Details: ${reason.message} " else " " } "
138
- throw ServiceConnectionHandshakeFailedException (message, e)
125
+ return if (receivedJson is JsonObject && " setupComplete" in receivedJson) {
126
+ LiveSession (
127
+ session = webSession,
128
+ blockingDispatcher = blockingDispatcher,
129
+ firebaseApp = firebaseApp
130
+ )
131
+ } else {
132
+ webSession.close()
133
+ throw ServiceConnectionHandshakeFailedException (" Unable to connect to the server" )
139
134
}
140
135
} catch (e: ClosedReceiveChannelException ) {
141
- throw ServiceConnectionHandshakeFailedException (" Channel was closed by the server." , e)
136
+ val reason = webSession?.closeReason?.await()
137
+ val message =
138
+ " Channel was closed by the server.${if (reason!= null ) " Details: ${reason.message} " else " " } "
139
+ throw ServiceConnectionHandshakeFailedException (message, e)
142
140
}
143
141
}
144
142
0 commit comments