Skip to content

Commit 41f7b9d

Browse files
committed
fix(leave): sending data after leaving the WebChannel
When did not yet join the WebChannel or just leaved the WebChannel, WebChannel.send WebChannel.sendTo methods throw an error. Now they execute successfully. When leaving the WebChannel, the channels have only been removed from the channel Set, but have not been closed. The peer who leaves the WebChannel also forgot to remove the channels from the Set.
1 parent 29e6bf1 commit 41f7b9d

File tree

5 files changed

+92
-44
lines changed

5 files changed

+92
-44
lines changed

dist/netflux.es2015.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2457,7 +2457,14 @@
24572457

24582458
/** Leave `WebChannel`. No longer can receive and send messages to the group. */
24592459
leave () {
2460-
this.manager.broadcast(this, formatter.msg(LEAVE, {id: this.myId}))
2460+
if (this.channels.size !== 0) {
2461+
this.manager.broadcast(this, formatter.msg(LEAVE, {id: this.myId}))
2462+
this.topology = this.settings.topology
2463+
this.channels.forEach((c) => {
2464+
c.close()
2465+
})
2466+
this.channels.clear()
2467+
}
24612468
}
24622469

24632470
/**
@@ -2466,9 +2473,11 @@
24662473
* @param {string} data Message
24672474
*/
24682475
send (data) {
2469-
formatter.handleUserMessage(data, this.myId, null, (dataChunk) => {
2470-
this.manager.broadcast(this, dataChunk)
2471-
})
2476+
if (this.channels.size !== 0) {
2477+
formatter.handleUserMessage(data, this.myId, null, (dataChunk) => {
2478+
this.manager.broadcast(this, dataChunk)
2479+
})
2480+
}
24722481
}
24732482

24742483
/**
@@ -2478,9 +2487,11 @@
24782487
* @param {type} data Message
24792488
*/
24802489
sendTo (id, data) {
2481-
formatter.handleUserMessage(data, this.myId, id, (dataChunk) => {
2482-
this.manager.sendTo(id, this, dataChunk)
2483-
})
2490+
if (this.channels.size !== 0) {
2491+
formatter.handleUserMessage(data, this.myId, id, (dataChunk) => {
2492+
this.manager.sendTo(id, this, dataChunk)
2493+
})
2494+
}
24842495
}
24852496

24862497
/**
@@ -2629,12 +2640,13 @@
26292640
let msg = formatter.readInternalMessage(data)
26302641
switch (header.code) {
26312642
case LEAVE:
2632-
this.onLeaving(msg.id)
26332643
for (let c of this.channels) {
26342644
if (c.peerId === msg.id) {
2645+
c.close()
26352646
this.channels.delete(c)
26362647
}
26372648
}
2649+
this.onLeaving(msg.id)
26382650
break
26392651
case SERVICE_DATA:
26402652
if (this.myId === msg.recepient) {
@@ -2808,7 +2820,7 @@
28082820
* @return {type} description
28092821
*/
28102822
generateKey () {
2811-
const MIN_LENGTH = 2
2823+
const MIN_LENGTH = 5
28122824
const DELTA_LENGTH = 0
28132825
const MASK = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
28142826
let result = ''

dist/netflux.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ return /******/ (function(modules) { // webpackBootstrap
259259
_createClass(WebChannel, [{
260260
key: 'leave',
261261
value: function leave() {
262-
this.manager.broadcast(this, formatter.msg(LEAVE, { id: this.myId }));
262+
if (this.channels.size !== 0) {
263+
this.manager.broadcast(this, formatter.msg(LEAVE, { id: this.myId }));
264+
this.topology = this.settings.topology;
265+
this.channels.forEach(function (c) {
266+
c.close();
267+
});
268+
this.channels.clear();
269+
}
263270
}
264271

265272
/**
@@ -273,9 +280,11 @@ return /******/ (function(modules) { // webpackBootstrap
273280
value: function send(data) {
274281
var _this = this;
275282

276-
formatter.handleUserMessage(data, this.myId, null, function (dataChunk) {
277-
_this.manager.broadcast(_this, dataChunk);
278-
});
283+
if (this.channels.size !== 0) {
284+
formatter.handleUserMessage(data, this.myId, null, function (dataChunk) {
285+
_this.manager.broadcast(_this, dataChunk);
286+
});
287+
}
279288
}
280289

281290
/**
@@ -290,9 +299,11 @@ return /******/ (function(modules) { // webpackBootstrap
290299
value: function sendTo(id, data) {
291300
var _this2 = this;
292301

293-
formatter.handleUserMessage(data, this.myId, id, function (dataChunk) {
294-
_this2.manager.sendTo(id, _this2, dataChunk);
295-
});
302+
if (this.channels.size !== 0) {
303+
formatter.handleUserMessage(data, this.myId, id, function (dataChunk) {
304+
_this2.manager.sendTo(id, _this2, dataChunk);
305+
});
306+
}
296307
}
297308

298309
/**
@@ -487,7 +498,6 @@ return /******/ (function(modules) { // webpackBootstrap
487498
var msg = formatter.readInternalMessage(data);
488499
switch (header.code) {
489500
case LEAVE:
490-
this.onLeaving(msg.id);
491501
var _iteratorNormalCompletion2 = true;
492502
var _didIteratorError2 = false;
493503
var _iteratorError2 = undefined;
@@ -497,6 +507,7 @@ return /******/ (function(modules) { // webpackBootstrap
497507
var c = _step2.value;
498508

499509
if (c.peerId === msg.id) {
510+
c.close();
500511
this.channels.delete(c);
501512
}
502513
}
@@ -515,6 +526,7 @@ return /******/ (function(modules) { // webpackBootstrap
515526
}
516527
}
517528

529+
this.onLeaving(msg.id);
518530
break;
519531
case SERVICE_DATA:
520532
if (this.myId === msg.recepient) {
@@ -779,7 +791,7 @@ return /******/ (function(modules) { // webpackBootstrap
779791
}, {
780792
key: 'generateKey',
781793
value: function generateKey() {
782-
var MIN_LENGTH = 2;
794+
var MIN_LENGTH = 5;
783795
var DELTA_LENGTH = 0;
784796
var MASK = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
785797
var result = '';

dist/netflux.min.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ return /******/ (function(modules) { // webpackBootstrap
259259
_createClass(WebChannel, [{
260260
key: 'leave',
261261
value: function leave() {
262-
this.manager.broadcast(this, formatter.msg(LEAVE, { id: this.myId }));
262+
if (this.channels.size !== 0) {
263+
this.manager.broadcast(this, formatter.msg(LEAVE, { id: this.myId }));
264+
this.topology = this.settings.topology;
265+
this.channels.forEach(function (c) {
266+
c.close();
267+
});
268+
this.channels.clear();
269+
}
263270
}
264271

265272
/**
@@ -273,9 +280,11 @@ return /******/ (function(modules) { // webpackBootstrap
273280
value: function send(data) {
274281
var _this = this;
275282

276-
formatter.handleUserMessage(data, this.myId, null, function (dataChunk) {
277-
_this.manager.broadcast(_this, dataChunk);
278-
});
283+
if (this.channels.size !== 0) {
284+
formatter.handleUserMessage(data, this.myId, null, function (dataChunk) {
285+
_this.manager.broadcast(_this, dataChunk);
286+
});
287+
}
279288
}
280289

281290
/**
@@ -290,9 +299,11 @@ return /******/ (function(modules) { // webpackBootstrap
290299
value: function sendTo(id, data) {
291300
var _this2 = this;
292301

293-
formatter.handleUserMessage(data, this.myId, id, function (dataChunk) {
294-
_this2.manager.sendTo(id, _this2, dataChunk);
295-
});
302+
if (this.channels.size !== 0) {
303+
formatter.handleUserMessage(data, this.myId, id, function (dataChunk) {
304+
_this2.manager.sendTo(id, _this2, dataChunk);
305+
});
306+
}
296307
}
297308

298309
/**
@@ -487,7 +498,6 @@ return /******/ (function(modules) { // webpackBootstrap
487498
var msg = formatter.readInternalMessage(data);
488499
switch (header.code) {
489500
case LEAVE:
490-
this.onLeaving(msg.id);
491501
var _iteratorNormalCompletion2 = true;
492502
var _didIteratorError2 = false;
493503
var _iteratorError2 = undefined;
@@ -497,6 +507,7 @@ return /******/ (function(modules) { // webpackBootstrap
497507
var c = _step2.value;
498508

499509
if (c.peerId === msg.id) {
510+
c.close();
500511
this.channels.delete(c);
501512
}
502513
}
@@ -515,6 +526,7 @@ return /******/ (function(modules) { // webpackBootstrap
515526
}
516527
}
517528

529+
this.onLeaving(msg.id);
518530
break;
519531
case SERVICE_DATA:
520532
if (this.myId === msg.recepient) {
@@ -779,7 +791,7 @@ return /******/ (function(modules) { // webpackBootstrap
779791
}, {
780792
key: 'generateKey',
781793
value: function generateKey() {
782-
var MIN_LENGTH = 2;
794+
var MIN_LENGTH = 5;
783795
var DELTA_LENGTH = 0;
784796
var MASK = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
785797
var result = '';

src/WebChannel.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ class WebChannel {
135135

136136
/** Leave `WebChannel`. No longer can receive and send messages to the group. */
137137
leave () {
138-
this.manager.broadcast(this, formatter.msg(LEAVE, {id: this.myId}))
138+
if (this.channels.size !== 0) {
139+
this.manager.broadcast(this, formatter.msg(LEAVE, {id: this.myId}))
140+
this.topology = this.settings.topology
141+
this.channels.forEach((c) => {
142+
c.close()
143+
})
144+
this.channels.clear()
145+
}
139146
}
140147

141148
/**
@@ -144,9 +151,11 @@ class WebChannel {
144151
* @param {string} data Message
145152
*/
146153
send (data) {
147-
formatter.handleUserMessage(data, this.myId, null, (dataChunk) => {
148-
this.manager.broadcast(this, dataChunk)
149-
})
154+
if (this.channels.size !== 0) {
155+
formatter.handleUserMessage(data, this.myId, null, (dataChunk) => {
156+
this.manager.broadcast(this, dataChunk)
157+
})
158+
}
150159
}
151160

152161
/**
@@ -156,9 +165,11 @@ class WebChannel {
156165
* @param {type} data Message
157166
*/
158167
sendTo (id, data) {
159-
formatter.handleUserMessage(data, this.myId, id, (dataChunk) => {
160-
this.manager.sendTo(id, this, dataChunk)
161-
})
168+
if (this.channels.size !== 0) {
169+
formatter.handleUserMessage(data, this.myId, id, (dataChunk) => {
170+
this.manager.sendTo(id, this, dataChunk)
171+
})
172+
}
162173
}
163174

164175
/**
@@ -307,12 +318,13 @@ class WebChannel {
307318
let msg = formatter.readInternalMessage(data)
308319
switch (header.code) {
309320
case LEAVE:
310-
this.onLeaving(msg.id)
311321
for (let c of this.channels) {
312322
if (c.peerId === msg.id) {
323+
c.close()
313324
this.channels.delete(c)
314325
}
315326
}
327+
this.onLeaving(msg.id)
316328
break
317329
case SERVICE_DATA:
318330
if (this.myId === msg.recepient) {
@@ -486,7 +498,7 @@ class WebChannel {
486498
* @return {type} description
487499
*/
488500
generateKey () {
489-
const MIN_LENGTH = 2
501+
const MIN_LENGTH = 5
490502
const DELTA_LENGTH = 0
491503
const MASK = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
492504
let result = ''

test/functional/fullyConnected/2peers.test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ describe('2 peers -> ', () => {
8282
wc2.onMessage = done.fail
8383
wc2.onLeaving = (id) => {
8484
expect(id).toBe(wc1.myId)
85-
// wc1.send(message)
86-
// wc1.sendTo(message)
85+
wc1.send(message)
86+
wc1.sendTo(message)
8787
done()
8888
}
8989

9090
wc1.openForJoining().then((data) => {
9191
wc2.join(data.key).catch(done.fail)
9292
}).catch(done.fail)
93-
// wc2.send(message)
94-
// wc2.sendTo(message)
93+
wc2.send(message)
94+
wc2.sendTo(message)
9595
})
9696

9797
it('guest is leaving', (done) => {
@@ -101,8 +101,8 @@ describe('2 peers -> ', () => {
101101
wc1.onMessage = done.fail
102102
wc1.onLeaving = (id) => {
103103
expect(id).toBe(wc2.myId)
104-
// wc1.send(message)
105-
// wc1.sendTo(message)
104+
wc1.send(message)
105+
wc1.sendTo(message)
106106
done()
107107
}
108108
let wc2 = new WebChannel({signaling})
@@ -113,8 +113,8 @@ describe('2 peers -> ', () => {
113113
wc2.join(data.key).then(() => wc2.leave())
114114
.catch(done.fail)
115115
}).catch(done.fail)
116-
// wc2.send(message)
117-
// wc2.sendTo(message)
116+
wc2.send(message)
117+
wc2.sendTo(message)
118118
})
119119
})
120120
})

0 commit comments

Comments
 (0)