Search…
Observe VPN Related Events
Provides a simple way to watch any events required by your applications integrating with VPN SDK.
The VPN SDK provides an observer interface that can be implemented in order to handle any events that occur from the client.
iOS
Android
The supported events that can be implemented are:

Status/Configuration Events

Property
Description
serverWillChange
The server defined in the client's configuration is about to change.
serverDidChange
The server defined in the client's configuration did change.
protocolWillChange
The protocol defined in the client's configuration is about to change.
protocolDidChange
The protocol defined in the client's configuration did change.

Connection Events

Property
Description
connectionWillBegin
The connection to the VPN server is about to start connecting.
connectionDidBegin
The connection to the VPN server did start connecting.
connectionWillReconnect
The connection to the VPN server is about to attempt to reconnect
connectionSucceeded
The connection to the VPN server was successful.
connectionFailed
The connection to the VPN server failed with error.
connectionWillDisconnect
The connection to the VPN server is about to disconnect.
connectionDidDisconnect
The connection to the VPN server did disconnect.

Miscellaneous Events

Property
Description
profileFailedToInstall
The iOS VPN profile failed to install and returned with error.
Called on connect(withConfiguration:completion:) if the VPN pop-up fails or is denied.
To subscribe to observing connection state events, use the subscribeToConnectionState method. An identifier of the subscription and an instance of a class that implements the ConnectionStateSubscriber interface must be passed in. This class will be notified of changes to the connection state when the connection traverses between DISCONNECTED, CONNECTING, CONNECTED and ERROR states.

Setting Up A Subscription

To setup the subscription, call the subscribeToConnectionState method as so:
1
val subscriptionId = UUID.randomUUID().toString()
2
val subscriber = object : ConnectionStateSubscriber {
3
override fun onStateChanged(newState: SudoVPNState) {
4
when (newState) {
5
SudoVPNState.CONNECTED -> {
6
// Handle change in connection state
7
}
8
SudoVPNState.CONNECTING -> {
9
// Handle change in connection state
10
}
11
SudoVPNState.DISCONNECTED -> {
12
// Handle change in connection state
13
}
14
SudoVPNState.ERROR -> {
15
// Handle change in connection state
16
}
17
}
18
}
19
}
20
launch {
21
try {
22
sudoVPNClient.subscribeToConnectionState(
23
subscriptionId,
24
subscriber
25
)
26
} catch (e: SudoVPNException) {
27
// Handle/notify the user of exception
28
}
29
}
Copied!

Cancelling A Subscription

To cancel a subscription, use the unsubscribeFromConnectionState or unsubscribeAll method. For example:
1
val subscriptionId = // From where the subscription was set up
2
launch {
3
try {
4
sudoVPNClient.unsubscribeFromConnectionState(subscriptionId)
5
// or
6
sudoVPNClient.unsubscribeAll()
7
} catch (e: SudoVPNException) {
8
// Handle/notify the user of exception
9
}
10
}
Copied!
This will ensure that the subscription is cancelled and system resources are freed.
Last modified 8mo ago
Copy link