Listening for Events
You can listen for SDK events by implementing the UpscopeListener interface and assigning it to the Upscope object:
Upscope.listener = object : UpscopeListener {
// Override methods you need
}
UpscopeListener Interface
All listener methods have default empty implementations, so you only need to override the ones you care about.
Upscope.listener = object : UpscopeListener {
override fun onConnectionStateChanged(state: ConnectionState) {
// Connection state changed
when (state) {
is ConnectionState.Inactive -> println("Inactive")
is ConnectionState.Connecting -> println("Connecting...")
is ConnectionState.Connected -> println("Connected")
is ConnectionState.Reconnecting -> println("Reconnecting...")
is ConnectionState.Error -> println("Error: ${state.error.message}")
}
}
override fun onSessionStarted(agentName: String?) {
println("Session started with ${agentName ?: "an agent"}")
}
override fun onSessionEnded(reason: SessionEndReason) {
when (reason) {
is SessionEndReason.UserStopped -> println("User ended session")
is SessionEndReason.AgentStopped -> println("Agent ended session")
is SessionEndReason.Timeout -> println("Session timed out")
is SessionEndReason.Error -> println("Session error: ${reason.error.message}")
}
}
override fun onCustomMessageReceived(message: String, observerId: String) {
println("Message from $observerId: $message")
}
override fun onError(error: UpscopeError) {
println("Error: ${error.code} - ${error.message}")
}
override fun onObserverJoined(observer: Observer) {
println("Observer joined: ${observer.name ?: observer.id}")
}
override fun onObserverLeft(observerId: String) {
println("Observer left: $observerId")
}
override fun onObserverCountChanged(count: Int) {
println("Observers: $count")
}
}
Event Reference
| Method | Description |
|---|---|
onConnectionStateChanged(state) | Called when the connection state changes. |
onSessionStarted(agentName) | A screen sharing session has started. agentName is the agent's display name if available. |
onSessionEnded(reason) | A session has ended. reason indicates why (user stopped, agent stopped, timeout, or error). |
onCustomMessageReceived(message, observerId) | A custom message was received from an observer. |
onError(error) | An SDK error occurred. |
onObserverJoined(observer) | An agent started observing the session. The Observer includes id, name, screen dimensions, and focus state. |
onObserverLeft(observerId) | An agent stopped observing the session. |
onObserverCountChanged(count) | The total number of active observers changed. |
