-
public final class WebSocketConnection extends BaseConnection
WebSocket transport for ElevenLabs Conversational AI.
Used for text-only sessions where LiveKit/WebRTC would tear down the room after a few seconds because no audio/video tracks are published. Speaks the same JSON event protocol as the existing data channel; serialization and parsing are reused from ConversationEventParser.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classWebSocketConnection.Companion
-
Field Summary
Fields Modifier and Type Field Description private final ConnectionStateconnectionState
-
Constructor Summary
Constructors Constructor Description WebSocketConnection(OkHttpClient client, CoroutineScope scope)
-
Method Summary
Modifier and Type Method Description ConnectionStategetConnectionState()Unitconnect(String serverUrl, ConversationConfig config)Establish a connection. Unitdisconnect(DisconnectionDetails details)Disconnect from the server and clean up resources UnitsendMessage(Object message)Send a message through the connection UnitsetOnMessageListener(Function1<String, Unit> listener)Set a listener for incoming messages UnitsetOnConnectionStateListener(Function1<ConnectionState, Unit> listener)Set a listener for connection state changes final Unitcleanup()-
-
Method Detail
-
getConnectionState
ConnectionState getConnectionState()
-
connect
Unit connect(String serverUrl, ConversationConfig config)
Establish a connection. Each transport reads its own credential from config: WebRTC uses ConversationConfig.conversationToken, WebSocket uses ConversationConfig.signedUrl (or builds a public-agent URL from ConversationConfig.agentId).
- Parameters:
serverUrl- URL of the server to connect toconfig- Conversation configuration
-
disconnect
Unit disconnect(DisconnectionDetails details)
Disconnect from the server and clean up resources
- Parameters:
details- Optional details about why the disconnection occurred.
-
sendMessage
Unit sendMessage(Object message)
Send a message through the connection
- Parameters:
message- The message to send (will be serialized as needed)
-
setOnMessageListener
Unit setOnMessageListener(Function1<String, Unit> listener)
Set a listener for incoming messages
- Parameters:
listener- Callback that receives incoming message strings
-
setOnConnectionStateListener
Unit setOnConnectionStateListener(Function1<ConnectionState, Unit> listener)
Set a listener for connection state changes
- Parameters:
listener- Callback that receives connection state updates
-
-
-
-