Created
March 15, 2016 14:37
-
-
Save Verikon/e04d911cbe1f27f47c86 to your computer and use it in GitHub Desktop.
safd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Invoke a method in an RPC ( REST-LIKE ) means. | |
@param String queue - the name of the queue youd like to invoke | |
@param Object message - the props object for this invocation. eg { username: 'bren', password: 'mypass' } | |
@param Function callback - OPTIONAL, a callback to fire with the response ; the response will be JSON parsed | |
@param Object options - OPTIONAL header options to pass on with your STOMP request - see https://www.rabbitmq.com/stomp.html for details | |
@return Promise - resolves with the JSON parsed reponse from the server. | |
**/ | |
invoke( queue, message, callback, options ) { | |
return new Promise( ( resolve, reject ) => { | |
options = options || {}; | |
options[ 'durable' ] = options.durable || false; | |
options[ 'auto-delete' ] = options.autoDelete || false; | |
options[ 'exclusive' ] = options.exclusive || false; | |
if( typeof message === 'object' ) | |
message = JSON.stringify( message ); | |
var responseQueue = 'RESP-' + parseInt( Math.random() * 10000000, 10 ); | |
options[ 'requeue' ] = responseQueue; | |
this.client.subscribe( '/queue/' + responseQueue, ( frame ) => { | |
var response; | |
if( frame.body && ( typeof frame.body === 'string' ) && ( frame.body.length > 2 ) ) | |
response = JSON.parse( frame.body ); | |
if( callback && typeof callback === 'function' ) | |
callback( response ); | |
this.client.unsubscribe( responseQueue ); | |
resolve( response ); | |
},{ id: responseQueue, 'auto-delete' : true }); | |
this.client.send( '/queue/' + queue, options, message ); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment