Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example for advertise_service and fix documentation #281

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;