I am attempting to build a custom chat interface for an Android application using Kotlin, bypassing the default Genesys Cloud Web Messaging widget. The goal is to manage the WebSocket connection directly to handle message events and user input. I have successfully obtained a guest token via POST /api/v2/conversations/webchat/guests and extracted the guestId and sessionId. I then initiate a WebSocket connection to the endpoint provided in the response, appending the guestId as a query parameter.
However, upon connecting, the initial handshake messages are not being processed correctly. I am sending the connect event with the guestId and sessionId, but I receive a 401 Unauthorized error in the WebSocket frame payload immediately after. My current implementation uses the okhttp3 library for WebSocket management. The code snippet for the connection setup is as follows:
val socket = client.newWebSocket(request, object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) {
val connectMessage = ConnectMessage(guestId, sessionId, "custom-ui-v1")
webSocket.send(gson.toJson(connectMessage))
}
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.e("GC_WS", "Connection failed: ${t.message}")
}
})
The ConnectMessage class matches the expected JSON structure from the documentation. Is there a specific sequence of messages required after the initial connect event that I am missing, or is the token validation failing due to a mismatch in the guestId format? I need to ensure the session is fully authenticated before sending any chat events.