Skip to content

Commit 80a2630

Browse files
authored
Merge pull request #47 from nicobritos/future_start_stop
Added Future to start and stop server
2 parents 0af9d5f + 2220b30 commit 80a2630

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

lib/src/server.dart

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/// 22/02/2017, Created by jumperchen
99
///
1010
/// Copyright (C) 2017 Potix Corporation. All Rights Reserved.
11+
import 'dart:async';
1112
import 'dart:io';
1213
import 'package:logging/logging.dart';
1314
import 'package:socket_io/src/client.dart';
@@ -40,6 +41,24 @@ class Server {
4041
StreamServer? httpServer;
4142
Engine? engine;
4243
Encoder encoder = Encoder();
44+
Future<bool>? _ready;
45+
46+
/// Server is ready
47+
///
48+
/// @return a Future that resolves to true whenever the server is ready
49+
/// @api public
50+
Future<bool> get ready => _ready ?? Future.value(false);
51+
52+
/// Server's port
53+
///
54+
/// @return the port number where the server is listening
55+
/// @api public
56+
int? get port {
57+
if (httpServer == null || httpServer!.channels.isEmpty) {
58+
return null;
59+
}
60+
return httpServer!.channels[0].port;
61+
}
4362

4463
/// Server constructor.
4564
///
@@ -54,7 +73,12 @@ class Server {
5473
origins(options.containsKey('origins') ? options['origins'] : '*:*');
5574
sockets = of('/');
5675
if (server != null) {
57-
attach(server, options);
76+
_ready = Future(() async {
77+
await attach(server, options);
78+
return true;
79+
});
80+
} else {
81+
_ready = Future.value(true);
5882
}
5983
}
6084

@@ -172,7 +196,6 @@ class Server {
172196
/// @param {String} origins
173197
/// @return {Server|Adapter} self when setting or value when getting
174198
/// @api public
175-
176199
dynamic origins([String? v]) {
177200
if (v == null || v.isEmpty) return _origins;
178201

@@ -186,8 +209,8 @@ class Server {
186209
/// @param {Object} options passed to engine.io
187210
/// @return {Server} self
188211
/// @api public
189-
void listen(srv, [Map? opts]) {
190-
attach(srv, opts);
212+
Future<void> listen(srv, [Map? opts]) async {
213+
await attach(srv, opts);
191214
}
192215

193216
/// Attaches socket.io to a server or port.
@@ -196,7 +219,7 @@ class Server {
196219
/// @param {Object} options passed to engine.io
197220
/// @return {Server} self
198221
/// @api public
199-
Server attach(dynamic srv, [Map? opts]) {
222+
Future<Server> attach(dynamic srv, [Map? opts]) async {
200223
if (srv is Function) {
201224
var msg = 'You are trying to attach socket.io to an express '
202225
'request handler function. Please pass a http.Server instance.';
@@ -221,7 +244,7 @@ class Server {
221244
_logger.fine('creating http server and binding to $srv');
222245
var port = srv.toInt();
223246
var server = StreamServer();
224-
server.start(port: port);
247+
await server.start(port: port);
225248
// HttpServer.bind(InternetAddress.ANY_IP_V4, port).then((
226249
// HttpServer server) {
227250
// this.httpServer = server;
@@ -231,6 +254,7 @@ class Server {
231254
//// response.close();
232255
//// });
233256
257+
var completer = Completer();
234258
var connectPacket = {'type': CONNECT, 'nsp': '/'};
235259
encoder.encode(connectPacket, (encodedPacket) {
236260
// the CONNECT packet will be merged with Engine.IO handshake,
@@ -249,7 +273,10 @@ class Server {
249273

250274
// bind to engine events
251275
bind(engine!);
276+
277+
completer.complete();
252278
});
279+
await completer.future;
253280
// });
254281
} else {
255282
var connectPacket = {'type': CONNECT, 'nsp': '/'};
@@ -351,7 +378,6 @@ class Server {
351378
/// @param {String} nsp name
352379
/// @param {Function} optional, nsp `connection` ev handler
353380
/// @api public
354-
355381
Namespace of(name, [fn]) {
356382
if (name.toString()[0] != '/') {
357383
name = '/' + name;
@@ -368,17 +394,20 @@ class Server {
368394

369395
/// Closes server connection
370396
///
397+
/// @return a Future that resolves when the httpServer is closed
371398
/// @api public
372-
void close() {
399+
Future<void> close() async {
373400
nsps['/']!.sockets.toList(growable: false).forEach((socket) {
374401
socket.onclose();
375402
});
376403

377404
engine?.close();
378405

379406
if (httpServer != null) {
380-
httpServer!.stop();
407+
await httpServer!.stop();
381408
}
409+
410+
_ready = null;
382411
}
383412

384413
// redirect to sockets method

0 commit comments

Comments
 (0)