Skip to content

Commit

Permalink
More tweaks to catch user connect/disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Rutland committed Feb 8, 2023
1 parent 9e13b18 commit 8dd0884
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lickdltd/chorus-dcl",
"version": "0.11.3",
"version": "0.11.4",
"description": "Lickd Chorus DCL library",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
54 changes: 29 additions & 25 deletions src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class Player extends Entity {

if (!this._connected) {
Logger.log('connection discarded - user has disconnected while retrieving token')
await this.disconnect(true)
this.reset()
return
}

Expand All @@ -198,12 +198,13 @@ export class Player extends Entity {

this.addComponentOrReplace(new utils.Interval(Player.HEARTBEAT_INTERVAL, () => this.heartbeat(this._token)))

Logger.log('connected successfully')

if (!this._connected) {
Logger.log('connection stream discarded - user has disconnected while connecting')
await this.disconnect(true)
this.reset()
return
}

Logger.log('connected successfully')
} catch (e) {
Logger.log('connection failed', e.message)
}
Expand Down Expand Up @@ -239,7 +240,7 @@ export class Player extends Entity {
} catch (e) {
Logger.log('heartbeat failed', e.message)

await this.disconnect(true)
this.reset()
await this.connect()
}
}
Expand All @@ -260,19 +261,24 @@ export class Player extends Entity {
}
}

private async disconnect(force: boolean = false): Promise<void> {
if (!this.isConnected()) {
Logger.log('disconnect skipped - not connected')
return
}

if (!force && this.isConnected() && (await this.isUserActive())) {
private async disconnect(): Promise<void> {
if (this.isConnected() && (await this.isUserActive())) {
Logger.log('disconnect skipped - connected and user still active (and not forced)')
return
}

Logger.log('disconnecting from', this._stream)

this.reset()

Logger.log('disconnected successfully')
}

private reset(): void {
Logger.log('resetting player')

this._connected = false

if (this.hasComponent(AudioStream)) {
this.removeComponent(AudioStream)
}
Expand All @@ -281,21 +287,19 @@ export class Player extends Entity {
this.removeComponent(utils.Interval)
}

this._connected = false

if (this._token !== undefined) {
this.signOut(this._token)
}
this.signOut()

Logger.log('disconnected successfully')
Logger.log('resetting player successful')
}

private signOut(token: string) {
signedFetch(this._url + '/api/listener/sign-out/dcl', {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify({ token })
}).catch((e) => Logger.log('sign out failed', e))
private signOut(): void {
if (this._token !== undefined) {
signedFetch(this._url + '/api/listener/sign-out/dcl', {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify({ token: this._token })
}).catch((e) => Logger.log('sign out failed', e))
}

this._token = undefined
}
Expand Down Expand Up @@ -351,6 +355,6 @@ export class Player extends Entity {
}

private isConnected(): boolean {
return this.hasComponent(AudioStream) || this._connected
return this.hasComponent(AudioStream)
}
}

0 comments on commit 8dd0884

Please sign in to comment.