Skip to content

Commit

Permalink
add example for advertise_service and fix documentation (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
T045T authored and Behery committed Jan 11, 2018
1 parent a0f5b04 commit 3bc4ec2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
18 changes: 18 additions & 0 deletions examples/simple.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@
console.log('Result for service call on ' + addTwoIntsClient.name + ': ' + result.sum);
});

// Advertising a Service
// ---------------------

// The Service object does double duty for both calling and advertising services
var setBoolServer = new ROSLIB.Service({
ros : ros,
name : '/set_bool',
serviceType : 'std_srvs/SetBool'
});

// Use the advertise() method to indicate that we want to provide this service
setBoolServer.advertise(function(request, response) {
console.log('Received service request on ' + setBoolServer.name + ': ' + request.data);
response['success'] = true;
response['message'] = 'Set successfully';
return true;
});

// Setting a param value
// ---------------------

Expand Down
17 changes: 11 additions & 6 deletions src/core/Service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function Service(options) {
}
Service.prototype.__proto__ = EventEmitter2.prototype;
/**
* Calls the service. Returns the service response in the callback.
* Calls the service. Returns the service response in the
* callback. Does nothing if this service is currently advertised.
*
* @param request - the ROSLIB.ServiceRequest to send
* @param callback - function with params:
Expand Down Expand Up @@ -64,11 +65,15 @@ Service.prototype.callService = function(request, callback, failedCallback) {
};

/**
* Every time a message is published for the given topic, the callback
* will be called with the message object.
* Advertise the service. This turns the Service object from a client
* into a server. The callback will be called with every request
* that's made on this service.
*
* @param callback - function with the following params:
* * message - the published message
* @param callback - This works similarly to the callback for a C++ service and should take the following params:
* * request - the service request
* * response - an empty dictionary. Take care not to overwrite this. Instead, only modify the values within.
* It should return true if the service has finished successfully,
* i.e. without any fatal errors.
*/
Service.prototype.advertise = function(callback) {
if (this.isAdvertised || typeof callback !== 'function') {
Expand Down Expand Up @@ -114,4 +119,4 @@ Service.prototype._serviceResponse = function(rosbridgeRequest) {
this.ros.callOnConnection(call);
};

module.exports = Service;
module.exports = Service;

0 comments on commit 3bc4ec2

Please sign in to comment.