108 lines
2.6 KiB
JavaScript
108 lines
2.6 KiB
JavaScript
/**
|
|
* Copyright (c) 2015-present, Peel Technologies, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* @providesModule TcpServer
|
|
* @flow
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var inherits = require('inherits');
|
|
var EventEmitter = require('events').EventEmitter;
|
|
var {
|
|
NativeModules
|
|
} = require('react-native');
|
|
var Sockets = NativeModules.TcpSockets;
|
|
|
|
var Socket = require('./TcpSocket');
|
|
|
|
function TcpServer(connectionListener: (socket: Socket) => void) {
|
|
if (!(this instanceof TcpServer)) {
|
|
return new TcpServer(connectionListener);
|
|
}
|
|
|
|
if (EventEmitter instanceof Function) {
|
|
EventEmitter.call(this);
|
|
}
|
|
|
|
var self = this;
|
|
|
|
this._socket = new Socket();
|
|
|
|
// $FlowFixMe: suppressing this error flow doesn't like EventEmitter
|
|
this._socket.on('connect', function() {
|
|
self.emit('listening');
|
|
});
|
|
// $FlowFixMe: suppressing this error flow doesn't like EventEmitter
|
|
this._socket.on('connection', function(socket) {
|
|
self._connections++;
|
|
self.emit('connection', socket);
|
|
});
|
|
// $FlowFixMe: suppressing this error flow doesn't like EventEmitter
|
|
this._socket.on('close', function() {
|
|
self.emit('close');
|
|
});
|
|
// $FlowFixMe: suppressing this error flow doesn't like EventEmitter
|
|
this._socket.on('error', function(error) {
|
|
self.emit('error', error);
|
|
});
|
|
|
|
if (typeof connectionListener === 'function') {
|
|
self.on('connection', connectionListener);
|
|
}
|
|
|
|
this._connections = 0;
|
|
}
|
|
|
|
inherits(TcpServer, EventEmitter);
|
|
|
|
TcpServer.prototype._debug = function() {
|
|
if (__DEV__) {
|
|
var args = [].slice.call(arguments);
|
|
console.log.apply(console, args);
|
|
}
|
|
};
|
|
|
|
// TODO : determine how to properly overload this with flow
|
|
TcpServer.prototype.listen = function() : TcpServer {
|
|
var args = this._socket._normalizeConnectArgs(arguments);
|
|
var options = args[0];
|
|
var callback = args[1];
|
|
|
|
var port = options.port;
|
|
var host = options.host || 'localhost';
|
|
|
|
if (callback) {
|
|
this.on('listening', callback);
|
|
}
|
|
|
|
this._socket._registerEvents();
|
|
Sockets.listen(this._socket._id, host, port);
|
|
|
|
return this;
|
|
};
|
|
|
|
TcpServer.prototype.getConnections = function(callback: (err: ?any, count: number) => void) {
|
|
if (typeof callback === 'function') {
|
|
callback.invoke(null, this._connections);
|
|
}
|
|
};
|
|
|
|
TcpServer.prototype.address = function() : { port: number, address: string, family: string } {
|
|
return this._socket.address();
|
|
};
|
|
|
|
TcpServer.prototype.close = function(callback: ?() => void) {
|
|
if (callback) {
|
|
this.on('close', callback);
|
|
}
|
|
|
|
this._socket.end();
|
|
};
|
|
|
|
// unimplemented net.Server apis
|
|
TcpServer.prototype.ref = TcpServer.prototype.unref = function() { /* nop */ };
|
|
|
|
module.exports = TcpServer;
|