mirror of https://github.com/grpc/grpc-node.git
Native: add Server#bindAsync
This commit is contained in:
parent
fae76dec4a
commit
5b85f4f2b7
|
@ -954,6 +954,7 @@ Server.prototype.addProtoService = util.deprecate(function(service,
|
||||||
* "address:port"
|
* "address:port"
|
||||||
* @param {grpc.ServerCredentials} creds Server credential object to be used for
|
* @param {grpc.ServerCredentials} creds Server credential object to be used for
|
||||||
* SSL. Pass an insecure credentials object for an insecure port.
|
* SSL. Pass an insecure credentials object for an insecure port.
|
||||||
|
* @return {number} The bound port number. Negative if binding the port failed.
|
||||||
*/
|
*/
|
||||||
Server.prototype.bind = function(port, creds) {
|
Server.prototype.bind = function(port, creds) {
|
||||||
if (this.started) {
|
if (this.started) {
|
||||||
|
@ -962,4 +963,32 @@ Server.prototype.bind = function(port, creds) {
|
||||||
return this._server.addHttp2Port(port, creds);
|
return this._server.addHttp2Port(port, creds);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called with the result of attempting to bind a port
|
||||||
|
* @callback grpc.Server~bindCallback
|
||||||
|
* @param {Error=} error If non-null, indicates that binding the port failed.
|
||||||
|
* @param {number} port The bound port number. If binding the port fails, this
|
||||||
|
* will be negative to match the output of bind.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Binds the server to the given port, with SSL disabled if creds is an
|
||||||
|
* insecure credentials object. Provides the result asynchronously.
|
||||||
|
* @param {string} port The port that the server should bind on, in the format
|
||||||
|
* "address:port"
|
||||||
|
* @param {grpc.ServerCredentials} creds Server credential object to be used for
|
||||||
|
* SSL. Pass an insecure credentials object for an insecure port.
|
||||||
|
*/
|
||||||
|
Server.prototype.bindAsync = function(port, creds, callback) {
|
||||||
|
/* This can throw. We do not try to catch that error because it indicates an
|
||||||
|
* incorrect use of the function, which should not be surfaced asynchronously
|
||||||
|
*/
|
||||||
|
const result = this.bind(port, creds)
|
||||||
|
if (result < 0) {
|
||||||
|
setImmediate(callback, new Error('Failed to bind port'), result);
|
||||||
|
} else {
|
||||||
|
setImmediate(callback, null, result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
exports.Server = Server;
|
exports.Server = Server;
|
||||||
|
|
|
@ -37,14 +37,16 @@ var getServer = require('./math/math_server.js');
|
||||||
|
|
||||||
var server = getServer();
|
var server = getServer();
|
||||||
|
|
||||||
|
let serverCreds = grpc.ServerCredentials.createInsecure();
|
||||||
|
|
||||||
describe('Async functionality', function() {
|
describe('Async functionality', function() {
|
||||||
before(function(done) {
|
before(function(done) {
|
||||||
var port_num = server.bind('0.0.0.0:0',
|
server.bind('0.0.0.0:0', serverCreds, (error, port_num) => {
|
||||||
grpc.ServerCredentials.createInsecure());
|
server.start();
|
||||||
server.start();
|
math_client = new math.Math('localhost:' + port_num,
|
||||||
math_client = new math.Math('localhost:' + port_num,
|
grpc.credentials.createInsecure());
|
||||||
grpc.credentials.createInsecure());
|
done();
|
||||||
done();
|
});
|
||||||
});
|
});
|
||||||
after(function() {
|
after(function() {
|
||||||
grpc.closeClient(math_client);
|
grpc.closeClient(math_client);
|
||||||
|
|
Loading…
Reference in New Issue