Detecting when clients have connected and disconnected
Learn how to detect when a client connects and disconnects from a session.
The Session object dispatches a connectionCreated event when a new client (including your own) connects to the session. The Session object dispatches a connectionDestroyed event when other clients leave the session. These events are defined by the ConnectionEvent class, which has a connection object, which is a Connection object for the connection (created or destroyed) related to the event:
var connectionCount;
session.on({
connectionCreated: function (event) {
connectionCount++;
if (event.connection.connectionId != session.connection.connectionId) {
console.log('Another client connected. ' + connectionCount + ' total.');
}
},
connectionDestroyed: function connectionDestroyedHandler(event) {
connectionCount--;
console.log('A client disconnected. ' + connectionCount + ' total.');
}
});
session.connect(token, function (error) {
if (error) {
console.log("Failed to connect.");
} else {
console.log('You have connected to the session.');
}
});
The OTSession object dispatches a connectionCreated event when a new client (excluding your own) connects to the session. The OTSession object dispatches a connectionDestroyed event when other clients leave the session. These events are defined by the ConnectionEvent class, which has a connection object, which is a Connection object for the connection (created or destroyed) related to the event:
let this.connectionCount = 0;
const this.sessionEventHandlers = {
connectionCreated: function (event) {
connectionCount++;
if (event.connection.connectionId != session.connection.connectionId) {
console.log('Another client connected. ' + connectionCount + ' total.');
}
},
connectionDestroyed: function connectionDestroyedHandler(event) {
connectionCount--;
console.log('A client disconnected. ' + connectionCount + ' total.');
}
sessionConnected: function (event) {
// include your own client's connection in the count
connectionCount++;
},
};
// reference later in JSX:
<OTSession
applicationId={this.apiKey}
sessionId={this.sessionId}
token={this.token}
eventHandlers={this.sessionEventHandlers}
>
{/* ... */}
When you are connected to a session, the Session.ConnectionListener.onConnectionCreated(Session session, Connection connection) method is called when a new client (other than your own) connects to the session.
The Session.ConnectionListener.onConnectionDestroyed(Session session, Connection connection) method is called when a client (other than your own) leaves the session. The Connection object passed into the method defines the connection hat has left the session.
Add a listener object for these connection events by calling the setConnectionListener(Session.ConnectionListener listener) method of the Session object:
mSession.setSessionListener(this);
@Override
public void onConnectionCreated(Session session, Connection connection)
{
// New client connected to the session
}
@Override
public void onConnectionDestroyed(Session session, Connection connection)
{
// A client disconnected from the session
}
The OTSessionDelegate session(_: connectionCreated:) message is sent to the session's delegate when another client connects to the session (and for each client connected to the session when you connect).
The OTSessionDelegate session(_: connectionDestroyed:) message is sent to the session's delegate when another client disconnects from the session.
The [OTSessionDelegate session:connectionCreated:] message is sent to the session's delegate when another client connects to the session (and for each client connected to the session when you connect).
The [OTSessionDelegate session:connectionDestroyed:] message is sent to the session's delegate when another client disconnects from the session.
When you are connected to a session, the Session.ConnectionCreated event is sent when a new client (other than your own) connects to the session. The ConnectionEventArgs object passed into the event listener defines the connection hat has left the session:
session.ConnectionCreated += Session_ConnectionCreated;
private void Session_ConnectionCreated(object sender, EventArgs e)
{
// Another client connected to the session.
}
The Session.ConnectionDropped event is sent when a client (other than your own) leaves the session. The ConnectionEventArgs object passed into the event listener defines the connection that has left the session.
session.ConnectionCreated += Session_ConnectionDropped;
private void Session_ConnectionDropped(object sender, EventArgs e)
{
// Another client disconnected from the session.
}
Once you have connected to a session, the on_connection_created callback function of the otc_session_callbacks struct is called when a new client (other than your own) connects to the session. The connection parameter of that function is a pointer to an instance of an otc_connection struct corresponding to the client connecting to the session.
The on_connection_dropped callback function of the otc_session_callbacks struct is called when a client (other than your own) disconnects from the session. The connection parameter of that function is a pointer to the otc_connection struct corresponding to the client disconnecting from the session.