Detecting when you have disconnected
You setup an EventListner to execute a function if a user is disconnected from a session.
For example, the function can notify the user whenever they loose connection and are no longer connected to the session.
When your client disconnects from a session, the OTSession component dispatches a sessionDisconnected event:
<OTSession
applicationId="your-api-key"
sessionId="your-session-id"
token="your-session-token"
eventHandlers={{
sessionDisconnected: event => {
console.log('disconnected', event);
},
connected: event => {
console.log('subscriber connected', event);
},
}}
>
<OTPublisher style={{ width: 100, height: 100 }}/>
<OTSubscriber style={{ width: 100, height: 100 }} />
</OTSession>
When your client disconnects from a session, the Session object dispatches a sessionDisconnected event:
session.on("sessionDisconnected", function (event) {
// The event is defined by the SessionDisconnectEvent class
if (event.reason == "networkDisconnected") {
alert("Your network connection terminated.")
}
});
session.connect(token);
The reason property of the event is a string that describes why the session disconnected. For example, the previous example notifies the user if they were disconnected due to the network connection terminating.
For details, see SessionDisconnectEvent.