From a9891b5018252503a0ae3b866daa8536bfce179c Mon Sep 17 00:00:00 2001 From: Dimitrie Stefanescu Date: Mon, 26 Nov 2018 11:47:58 +0000 Subject: [PATCH] changes in how ws connections are handled and disposed of --- speckle-plugin-manifest.json | 3 ++- src/components/SpeckleReceiverCard.vue | 23 ++++++++++++----------- src/receiver/ClientReceiver.js | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/speckle-plugin-manifest.json b/speckle-plugin-manifest.json index b041417..eeb56ef 100644 --- a/speckle-plugin-manifest.json +++ b/speckle-plugin-manifest.json @@ -5,5 +5,6 @@ "serveFrom": "/view", "author": "Speckle Project Contributors", "contact":"hello@speckle.works", - "homepage":"https://speckle.works" + "homepage":"https://speckle.works", + "git":"https://github.com/speckleworks/SpeckleViewer" } diff --git a/src/components/SpeckleReceiverCard.vue b/src/components/SpeckleReceiverCard.vue index 8e62a90..bfa638b 100644 --- a/src/components/SpeckleReceiverCard.vue +++ b/src/components/SpeckleReceiverCard.vue @@ -250,6 +250,7 @@ export default { if ( this.currentComputeResponse !== null ) bus.$emit( 'r-unload-objects', { objs: this.currentComputeResponse.objects.map( o => o._id ), streamId: this.currentComputeResponse.streamId } ) + this.myClientReceiver.disposeClient( ) this.$store.commit( 'DROP_RECEIVER', { streamId } ) this.updateUrl( ) }, @@ -265,7 +266,7 @@ export default { getControllers( ) { console.log( 'Getting controllers for ' + this.spkreceiver.streamId ) - this.mySpkReceiver.broadcast( { eventType: 'get-definition-io' } ) + this.myClientReceiver.broadcast( { eventType: 'get-definition-io' } ) }, addControllers( wsMessage ) { @@ -277,7 +278,7 @@ export default { controllersChanged( ) { console.log( 'controllers changed', this.controllers ) this.showComputeProgressBar = true - let args = { controllers: this.controllers, layers: this.spkreceiver.layers, client: this.mySpkReceiver, senderId: this.senderId } + let args = { controllers: this.controllers, layers: this.spkreceiver.layers, client: this.myClientReceiver, senderId: this.senderId } this.computeInProgress = true this.sendComputeRequest( args ) }, @@ -360,7 +361,7 @@ export default { newQuery += 'streams=' + streams // 3. get that in the url bar history.replaceState( { spk: 'changed history' }, "Speckle Viewer Rocks", newQuery ) - // if no query, just barge in and add the streams list, but do check if we have any streams to actually add + // if no query, just barge in and add the streams list, but do check if we have any streams to actually add } else if ( this.$store.getters.allReceivers.length !== 0 ) { history.replaceState( { spk: 'changed history' }, "Speckle Viewer Rocks", '?streams=' + streams ) } @@ -371,18 +372,18 @@ export default { console.log( 'Stream receiver mounted for streamid: ' + this.spkreceiver.streamId ) this.name = 'loading ' + this.spkreceiver.streamId - this.mySpkReceiver = new ClientReceiver( { + this.myClientReceiver = new ClientReceiver( { baseUrl: this.spkreceiver.serverUrl, streamId: this.spkreceiver.streamId, - token: this.spkreceiver.token + token: this.$store.state.jwtToken } ) - this.mySpkReceiver.on( 'error', this.receiverError ) - this.mySpkReceiver.on( 'ready', this.receiverReady ) - this.mySpkReceiver.on( 'update-meta', this.updateMeta ) - this.mySpkReceiver.on( 'update-global', this.updateGlobal ) - this.mySpkReceiver.on( 'get-def-io-response', this.addControllers ) - this.mySpkReceiver.on( 'compute-response', this.computeResponse ) + this.myClientReceiver.on( 'error', this.receiverError ) + this.myClientReceiver.on( 'ready', this.receiverReady ) + this.myClientReceiver.on( 'update-meta', this.updateMeta ) + this.myClientReceiver.on( 'update-global', this.updateGlobal ) + this.myClientReceiver.on( 'get-def-io-response', this.addControllers ) + this.myClientReceiver.on( 'compute-response', this.computeResponse ) } } diff --git a/src/receiver/ClientReceiver.js b/src/receiver/ClientReceiver.js index ed049ac..2e7874e 100644 --- a/src/receiver/ClientReceiver.js +++ b/src/receiver/ClientReceiver.js @@ -10,7 +10,7 @@ export default class SpeckleReceiver extends EventEmitter { if ( !args.baseUrl ) throw new Error( 'No stream id provided' ) this.baseUrl = args.baseUrl - this.auth = args.auth + this.token = args.token this.streamId = args.streamId this.wsUrl = this.baseUrl.replace( 'http', 'ws' ) @@ -27,7 +27,7 @@ export default class SpeckleReceiver extends EventEmitter { // registers an anonymous client setupClient( cb ) { - axios.post( this.baseUrl + '/clients', { client: { documentName: 'Online Viewer' } }, { headers: { 'Auth': this.auth } } ) + axios.post( this.baseUrl + '/clients', { client: { documentName: 'Online Viewer' } }, { headers: { 'Authorization': this.token } } ) .then( response => { this.clientId = response.data.resource._id cb( ) @@ -37,9 +37,20 @@ export default class SpeckleReceiver extends EventEmitter { } ) } + disposeClient( cb ) { + axios.delete( `${this.baseUrl}/clients/${this.clientId}` ) + .then( response => { + this.clientId = null + this.ws.close( ) + } ) + .catch( err => { + console.log( err ) + } ) + } + // sets up websockets & ws events setupWebsockets( cb ) { - this.ws = new WebSocket( this.wsUrl + '/?access_token=' + this.auth + '&stream_id=' + this.streamId + '&client_id=' + this.clientId ) + this.ws = new WebSocket( this.wsUrl + '/?access_token=' + this.token + '&stream_id=' + this.streamId + '&client_id=' + this.clientId ) this.ws.onopen = ( ) => { console.log( 'Websocket connection opened for', this.streamId )