Skip to content
This repository has been archived by the owner on May 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #83 from speckleworks/Dim/dev/client-ws-fixes
Browse files Browse the repository at this point in the history
changes in how ws connections are handled and disposed of
  • Loading branch information
didimitrie authored Nov 26, 2018
2 parents 45ff8fe + a9891b5 commit 0df0ce7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
3 changes: 2 additions & 1 deletion speckle-plugin-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"serveFrom": "/view",
"author": "Speckle Project Contributors",
"contact":"[email protected]",
"homepage":"https://speckle.works"
"homepage":"https://speckle.works",
"git":"https://github.com/speckleworks/SpeckleViewer"
}
23 changes: 12 additions & 11 deletions src/components/SpeckleReceiverCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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( )
},
Expand All @@ -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 ) {
Expand All @@ -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 )
},
Expand Down Expand Up @@ -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 )
}
Expand All @@ -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 )
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/receiver/ClientReceiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' )
Expand All @@ -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( )
Expand All @@ -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 )
Expand Down

0 comments on commit 0df0ce7

Please sign in to comment.