Skip to content

Commit 3bc4ec2

Browse files
T045TBehery
authored andcommitted
add example for advertise_service and fix documentation (#281)
1 parent a0f5b04 commit 3bc4ec2

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

examples/simple.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@
108108
console.log('Result for service call on ' + addTwoIntsClient.name + ': ' + result.sum);
109109
});
110110

111+
// Advertising a Service
112+
// ---------------------
113+
114+
// The Service object does double duty for both calling and advertising services
115+
var setBoolServer = new ROSLIB.Service({
116+
ros : ros,
117+
name : '/set_bool',
118+
serviceType : 'std_srvs/SetBool'
119+
});
120+
121+
// Use the advertise() method to indicate that we want to provide this service
122+
setBoolServer.advertise(function(request, response) {
123+
console.log('Received service request on ' + setBoolServer.name + ': ' + request.data);
124+
response['success'] = true;
125+
response['message'] = 'Set successfully';
126+
return true;
127+
});
128+
111129
// Setting a param value
112130
// ---------------------
113131

src/core/Service.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ function Service(options) {
2727
}
2828
Service.prototype.__proto__ = EventEmitter2.prototype;
2929
/**
30-
* Calls the service. Returns the service response in the callback.
30+
* Calls the service. Returns the service response in the
31+
* callback. Does nothing if this service is currently advertised.
3132
*
3233
* @param request - the ROSLIB.ServiceRequest to send
3334
* @param callback - function with params:
@@ -64,11 +65,15 @@ Service.prototype.callService = function(request, callback, failedCallback) {
6465
};
6566

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

117-
module.exports = Service;
122+
module.exports = Service;

0 commit comments

Comments
 (0)