in packages/sdk/src/internal/adapters/multipeer/session.ts [182:222]
private setAuthoritativeClient(clientId: Guid) {
const newAuthority = this._clientSet.get(clientId);
if (!newAuthority) {
log.error('network', `[ERROR] setAuthoritativeClient: client ${clientId} does not exist.`);
return;
}
const oldAuthority = this.authoritativeClient;
newAuthority.setAuthoritative(true);
for (const client of this.clients.filter(c => c !== newAuthority)) {
client.setAuthoritative(false);
}
// if client user id is known emit signal about authoritative simulation owner
if (newAuthority.userId) {
newAuthority.session.emit('set-authoritative', newAuthority.userId);
}
// forward connection quality metrics
if (this.conn instanceof EventedConnection) {
this.conn.linkConnectionQuality(newAuthority.conn.quality);
}
// forward network stats from the authoritative peer connection to the app
const toApp = this.conn instanceof EventedConnection ? this.conn : null;
const forwardIncoming = (bytes: number) => toApp.statsTracker.recordIncoming(bytes);
const forwardOutgoing = (bytes: number) => toApp.statsTracker.recordOutgoing(bytes);
const toNewAuthority = newAuthority.conn instanceof EventedConnection ? newAuthority.conn : null;
if (toNewAuthority) {
toNewAuthority.statsTracker.on('incoming', forwardIncoming);
toNewAuthority.statsTracker.on('outgoing', forwardOutgoing);
}
// turn off old authority
const toOldAuthority = oldAuthority && oldAuthority.conn instanceof EventedConnection
? oldAuthority.conn : null;
if (toOldAuthority) {
toOldAuthority.statsTracker.off('incoming', forwardIncoming);
toOldAuthority.statsTracker.off('outgoing', forwardOutgoing);
}
}