forked from PeelTechnologies/react-native-tcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpServer.js
116 lines (92 loc) · 2.74 KB
/
TcpServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Copyright (c) 2015-present, Peel Technologies, Inc.
* All rights reserved.
*
* @providesModule TcpServer
* @flow
*/
'use strict';
var util = require('util');
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('error', function(error) {
self.emit('error', error);
});
if (typeof connectionListener === 'function') {
self.on('connection', connectionListener);
}
this._connections = 0;
}
util.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 || '0.0.0.0';
if (callback) {
this.once('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 ? this._socket.address() : {};
};
TcpServer.prototype.close = function(callback: ?() => void) {
if (typeof callback === 'function') {
if (!this._socket) {
this.once('close', function close() {
callback(new Error('Not running'));
});
} else {
this.once('close', callback);
}
}
if (this._socket) {
this._socket.end();
}
var self = this;
setImmediate(function () {
self.emit('close');
});
};
// unimplemented net.Server apis
TcpServer.prototype.ref = TcpServer.prototype.unref = function() { /* nop */ };
module.exports = TcpServer;