Skip to content

Commit f06d3a4

Browse files
committed
Fix #172 socket id's not synced
1 parent ad5b665 commit f06d3a4

File tree

5 files changed

+94
-113
lines changed

5 files changed

+94
-113
lines changed

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## 2.0.0-beta.4-nullsafety.0
2+
3+
**New Feature:**
4+
5+
* [#177](https://github.com/rikulo/socket.io-client-dart/pull/177) Send credentials with the auth option
6+
7+
**Bug fix:**
8+
9+
* [#172](https://github.com/rikulo/socket.io-client-dart/issues/172) socket id's not synced
10+
111
## 2.0.0-beta.3-nullsafety.0
212

313
**New Feature:**

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,4 @@ If you are new to Git or GitHub, please read [this guide](https://help.github.co
275275
- Thanks [@bruce3x](https://github.com/bruce3x) for https://github.com/rikulo/socket.io-client-dart/issues/25
276276
- Thanks [@Kavantix](https://github.com/Kavantix) for https://github.com/rikulo/socket.io-client-dart/issues/26
277277
- Thanks [@luandnguyen](https://github.com/luandnguyen) for https://github.com/rikulo/socket.io-client-dart/issues/59
278+
- Thanks [@jorgefspereira](https://github.com/jorgefspereira) for https://github.com/rikulo/socket.io-client-dart/pull/177

lib/src/manager.dart

+29-95
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,9 @@ class Manager extends EventEmitter {
6666
_Backoff? backoff;
6767
String readyState = 'closed';
6868
late String uri;
69-
List connecting = [];
70-
num? lastPing;
71-
bool encoding = false;
72-
List packetBuffer = [];
7369
bool reconnecting = false;
7470

75-
late engine_socket.Socket engine;
71+
engine_socket.Socket? engine;
7672
Encoder encoder = Encoder();
7773
Decoder decoder = Decoder();
7874
late bool autoConnect;
@@ -98,41 +94,6 @@ class Manager extends EventEmitter {
9894
if (autoConnect) open();
9995
}
10096

101-
///
102-
/// Propagate given event to sockets and emit on `this`
103-
///
104-
/// @api private
105-
///
106-
void emitAll(String event, [data]) {
107-
emit(event, data);
108-
for (var nsp in nsps.keys) {
109-
nsps[nsp]!.emit(event, data);
110-
}
111-
}
112-
113-
///
114-
/// Update `socket.id` of all sockets
115-
///
116-
/// @api private
117-
///
118-
void updateSocketIds() {
119-
for (var nsp in nsps.keys) {
120-
nsps[nsp]!.id = generateId(nsp);
121-
}
122-
}
123-
124-
///
125-
/// generate `socket.id` for the given `nsp`
126-
///
127-
/// @param {String} nsp
128-
/// @return {String}
129-
/// @api private
130-
///
131-
String generateId(String nsp) {
132-
if (nsp.startsWith('/')) nsp = nsp.substring(1);
133-
return (nsp.isEmpty ? '' : (nsp + '#')) + (engine.id ?? '');
134-
}
135-
13697
num? get randomizationFactor => _randomizationFactor;
13798
set randomizationFactor(num? v) {
13899
_randomizationFactor = v;
@@ -182,12 +143,12 @@ class Manager extends EventEmitter {
182143

183144
_logger.fine('opening $uri');
184145
engine = engine_socket.Socket(uri, options);
185-
var socket = engine;
146+
var socket = engine!;
186147
readyState = 'opening';
187148
skipReconnect = false;
188149

189150
// emit `open`
190-
var openSub = util.on(socket, 'open', (_) {
151+
var openSubDestroy = util.on(socket, 'open', (_) {
191152
onopen();
192153
if (callback != null) callback();
193154
});
@@ -197,7 +158,7 @@ class Manager extends EventEmitter {
197158
_logger.fine('connect_error');
198159
cleanup();
199160
readyState = 'closed';
200-
emitAll('connect_error', data);
161+
super.emit('error', data);
201162
if (callback != null) {
202163
callback({'error': 'Connection error', 'data': data});
203164
} else {
@@ -210,19 +171,22 @@ class Manager extends EventEmitter {
210171
if (timeout != null) {
211172
_logger.fine('connect attempt will timeout after $timeout');
212173

174+
if (timeout == 0) {
175+
openSubDestroy
176+
.destroy(); // prevents a race condition with the 'open' event
177+
}
213178
// set timer
214179
var timer = Timer(Duration(milliseconds: timeout!.toInt()), () {
215180
_logger.fine('connect attempt timed out after $timeout');
216-
openSub.destroy();
181+
openSubDestroy.destroy();
217182
socket.close();
218183
socket.emit('error', 'timeout');
219-
emitAll('connect_timeout', timeout);
220184
});
221185

222186
subs.add(Destroyable(() => timer.cancel()));
223187
}
224188

225-
subs.add(openSub);
189+
subs.add(openSubDestroy);
226190
subs.add(errorSub);
227191

228192
return this;
@@ -244,7 +208,7 @@ class Manager extends EventEmitter {
244208
emit('open');
245209

246210
// add subs
247-
var socket = engine;
211+
var socket = engine!;
248212
subs.add(util.on(socket, 'data', ondata));
249213
subs.add(util.on(socket, 'ping', onping));
250214
// subs.add(util.on(socket, 'pong', onpong));
@@ -259,8 +223,7 @@ class Manager extends EventEmitter {
259223
/// @api private
260224
///
261225
void onping([_]) {
262-
lastPing = DateTime.now().millisecondsSinceEpoch;
263-
emitAll('ping');
226+
emit('ping');
264227
}
265228

266229
///
@@ -297,7 +260,7 @@ class Manager extends EventEmitter {
297260
///
298261
void onerror(err) {
299262
_logger.fine('error $err');
300-
emitAll('error', err);
263+
emit('error', err);
301264
}
302265

303266
///
@@ -309,24 +272,9 @@ class Manager extends EventEmitter {
309272
Socket socket(String nsp, Map opts) {
310273
var socket = nsps[nsp];
311274

312-
var onConnecting = ([_]) {
313-
if (!connecting.contains(socket)) {
314-
connecting.add(socket);
315-
}
316-
};
317-
318275
if (socket == null) {
319276
socket = Socket(this, nsp, opts);
320277
nsps[nsp] = socket;
321-
socket.on('connecting', onConnecting);
322-
socket.on('connect', (_) {
323-
socket!.id = generateId(nsp);
324-
});
325-
326-
if (autoConnect) {
327-
// manually call here since connecting event is fired before listening
328-
onConnecting();
329-
}
330278
}
331279

332280
return socket;
@@ -338,8 +286,16 @@ class Manager extends EventEmitter {
338286
/// @param {Socket} socket
339287
///
340288
void destroy(socket) {
341-
connecting.remove(socket);
342-
if (connecting.isNotEmpty) return;
289+
final nsps = this.nsps.keys;
290+
291+
for (var nsp in nsps) {
292+
final socket = this.nsps[nsp];
293+
294+
if (socket!.active) {
295+
_logger.fine('socket $nsp is still active, skipping close');
296+
return;
297+
}
298+
}
343299

344300
close();
345301
}
@@ -352,37 +308,21 @@ class Manager extends EventEmitter {
352308
///
353309
void packet(Map packet) {
354310
_logger.fine('writing packet $packet');
355-
if (packet.containsKey('query') && packet['type'] == 0) {
356-
packet['nsp'] += '''?${packet['query']}''';
357-
}
358311

359312
// if (encoding != true) {
360313
// encode, then write to engine with result
361314
// encoding = true;
362315
var encodedPackets = encoder.encode(packet);
363316

364317
for (var i = 0; i < encodedPackets.length; i++) {
365-
engine.write(encodedPackets[i], packet['options']);
318+
engine!.write(encodedPackets[i], packet['options']);
366319
}
367320
// } else {
368321
// add packet to the queue
369322
// packetBuffer.add(packet);
370323
// }
371324
}
372325

373-
///
374-
/// If packet buffer is non-empty, begins encoding the
375-
/// next packet in line.
376-
///
377-
/// @api private
378-
///
379-
void processPacketQueue() {
380-
if (packetBuffer.isNotEmpty && encoding != true) {
381-
var pack = packetBuffer.removeAt(0);
382-
packet(pack);
383-
}
384-
}
385-
386326
///
387327
/// Clean up transport subscriptions and packet buffer.
388328
///
@@ -397,10 +337,6 @@ class Manager extends EventEmitter {
397337
sub.destroy();
398338
}
399339

400-
packetBuffer = [];
401-
encoding = false;
402-
lastPing = null;
403-
404340
decoder.destroy();
405341
}
406342

@@ -422,7 +358,7 @@ class Manager extends EventEmitter {
422358
}
423359
backoff!.reset();
424360
readyState = 'closed';
425-
engine.close();
361+
engine?.close();
426362
}
427363

428364
///
@@ -454,7 +390,7 @@ class Manager extends EventEmitter {
454390
if (backoff!.attempts >= reconnectionAttempts!) {
455391
_logger.fine('reconnect failed');
456392
backoff!.reset();
457-
emitAll('reconnect_failed');
393+
emit('reconnect_failed');
458394
reconnecting = false;
459395
} else {
460396
var delay = backoff!.duration;
@@ -465,8 +401,7 @@ class Manager extends EventEmitter {
465401
if (skipReconnect!) return;
466402

467403
_logger.fine('attempting reconnect');
468-
emitAll('reconnect_attempt', backoff!.attempts);
469-
emitAll('reconnecting', backoff!.attempts);
404+
emit('reconnect_attempt', backoff!.attempts);
470405

471406
// check again for the case socket closed in above events
472407
if (skipReconnect!) return;
@@ -476,7 +411,7 @@ class Manager extends EventEmitter {
476411
_logger.fine('reconnect attempt error');
477412
reconnecting = false;
478413
reconnect();
479-
emitAll('reconnect_error', err['data']);
414+
emit('reconnect_error', err['data']);
480415
} else {
481416
_logger.fine('reconnect success');
482417
onreconnect();
@@ -498,8 +433,7 @@ class Manager extends EventEmitter {
498433
var attempt = backoff!.attempts;
499434
reconnecting = false;
500435
backoff!.reset();
501-
updateSocketIds();
502-
emitAll('reconnect', attempt);
436+
emit('reconnect', attempt);
503437
}
504438
}
505439

0 commit comments

Comments
 (0)