Skip to content

Commit 75c358b

Browse files
committed
fix(message): issue with sending messages > 15kb
1 parent be3d44f commit 75c358b

File tree

6 files changed

+129
-3
lines changed

6 files changed

+129
-3
lines changed

src/service/UserMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export class UserMessage extends Service<proto.IMessage, proto.Message> {
5454
const length = Math.min(MAX_USER_MSG_SIZE, bytes.length - MAX_USER_MSG_SIZE * i)
5555
const begin = MAX_USER_MSG_SIZE * i
5656
const end = begin + length
57-
msg.chunk.number = i
57+
msg.chunk.nb = i
5858
msg.chunk.content = new Uint8Array(bytes.slice(begin, end))
5959
res[i] = super.encode(msg)
6060
}

test/functional/2-bot_client.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
botJoin,
99
botLeave,
1010
cleanWebGroup,
11+
copyArrayBuffer,
12+
randomBigArrayBuffer,
1113
randomKey,
1214
SIGNALING_URL,
1315
wait,
@@ -360,6 +362,30 @@ describe('🤖 🙂 - 2 members: bot first, then client', () => {
360362
client.send(msgClient)
361363
})
362364

365+
/** @test {WebGroup#sendTo} */
366+
it('broadcast message cutted in chunks (> 15kb)', (done) => {
367+
const msgClient = randomBigArrayBuffer()
368+
const msgBot = copyArrayBuffer(msgClient)
369+
msgBot[0] = 42
370+
371+
// Check bot bot
372+
client.onMessage = (id, msg) => {
373+
called++
374+
expect(msg).toEqual(msgBot)
375+
376+
// Check bot bot
377+
wait(1000)
378+
.then((bot) => {
379+
expect(called).toEqual(1)
380+
done()
381+
})
382+
.catch(fail)
383+
}
384+
385+
// Start sending message
386+
client.send(msgClient)
387+
})
388+
363389
/** @test {WebGroup#sendTo} */
364390
it('private String', (done) => {
365391
const msgClient = 'Art is long, life is short'

test/functional/2-client_bot.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
cleanWebGroup,
1111
IBotData,
1212
Queue,
13+
randomBigArrayBuffer,
1314
SIGNALING_URL,
1415
wait,
1516
} from '../util/helper'
@@ -378,6 +379,25 @@ describe('🙂 🤖 - 2 members: client invites bot', () => {
378379
client.send(msg1)
379380
})
380381

382+
/** @test {WebGroup#sendTo} */
383+
it('broadcast message cutted in chunks (> 15kb)', (done) => {
384+
const bytes = randomBigArrayBuffer()
385+
386+
// Check bot bot
387+
wait(1000)
388+
.then(() => botGetData(client.key))
389+
.then((bot) => {
390+
expect(bot.onMessageToBeCalled).toEqual(1)
391+
expect(bot.messages[0].msg).toEqual(Array.from(bytes) as any)
392+
expect(bot.messages[0].id).toEqual(client.myId)
393+
done()
394+
})
395+
.catch(fail)
396+
397+
// Start sending message
398+
client.send(bytes)
399+
})
400+
381401
/** @test {WebGroup#sendTo} */
382402
it('private String', (done) => {
383403
const msg1 = 'Art is long, life is short'

test/functional/2-clients.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
/// <reference types='jasmine' />
22

33
import { SignalingState, Topology, WebGroup, WebGroupState } from '../../src/index.browser'
4-
import { areTheSame, cleanWebGroup, Queue, SIGNALING_URL, wait } from '../util/helper'
4+
import {
5+
areTheSame,
6+
cleanWebGroup,
7+
Queue,
8+
randomBigArrayBuffer,
9+
SIGNALING_URL,
10+
wait,
11+
} from '../util/helper'
512

613
const WebGroupOptions = {
714
signalingServer: SIGNALING_URL,
@@ -455,6 +462,26 @@ describe('🙂 🙂 - 2 clients', () => {
455462
wg2.send(msg2)
456463
})
457464

465+
/** @test {WebGroup#sendTo} */
466+
it('broadcast message cutted in chunks (> 15kb)', (done) => {
467+
const bytes = randomBigArrayBuffer()
468+
469+
// Code for peer 1
470+
wg1.onMessage = (id, msg) => {
471+
expect(id).toEqual(wg2.myId)
472+
expect(msg instanceof Uint8Array)
473+
expect(msg).toEqual(bytes)
474+
called1++
475+
wait(1000).then(() => {
476+
expect(called1).toEqual(1)
477+
done()
478+
})
479+
}
480+
481+
// Start sending message
482+
wg2.send(bytes)
483+
})
484+
458485
/** @test {WebGroup#sendTo} */
459486
it('private String', (done) => {
460487
const msg1 = 'Art is long, life is short'

test/functional/3-clients.test.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
/// <reference types='jasmine' />
22
/* tslint:disable:one-variable-per-declaration */
33
import { SignalingState, WebGroup, WebGroupState } from '../../src/index.browser'
4-
import { areTheSame, cleanWebGroup, IMessages, Queue, SIGNALING_URL, wait } from '../util/helper'
4+
import {
5+
areTheSame,
6+
cleanWebGroup,
7+
IMessages,
8+
Queue,
9+
randomBigArrayBuffer,
10+
SIGNALING_URL,
11+
wait,
12+
} from '../util/helper'
513

614
const WebGroupOptions = {
715
signalingServer: SIGNALING_URL,
@@ -343,6 +351,37 @@ describe('🙂 🙂 🙂 - 3 clients', () => {
343351
wg3.send(msg3)
344352
})
345353

354+
/** @test {WebGroup#sendTo} */
355+
it('broadcast message cutted in chunks (> 15kb)', (done) => {
356+
const bytes = randomBigArrayBuffer()
357+
const queue = new Queue(2, () => {
358+
wait(1000).then(() => {
359+
expect(called2).toEqual(1)
360+
expect(called3).toEqual(1)
361+
done()
362+
})
363+
})
364+
365+
// Code for peer 2
366+
wg2.onMessage = (id, msg) => {
367+
expect(msg instanceof Uint8Array).toBeTruthy()
368+
expect(msg).toEqual(bytes)
369+
called2++
370+
queue.done()
371+
}
372+
373+
// Code for peer 3
374+
wg3.onMessage = (id, msg) => {
375+
expect(msg instanceof Uint8Array).toBeTruthy()
376+
expect(msg).toEqual(bytes)
377+
called3++
378+
queue.done()
379+
}
380+
381+
// Start sending message
382+
wg1.send(bytes)
383+
})
384+
346385
/** @test {WebGroup#sendTo} */
347386
it('private String', (done) => {
348387
const queue = new Queue(6, () => {

test/util/helper.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ export function randomKey(): string {
3333
return result
3434
}
3535

36+
export function randomBigArrayBuffer(): Uint8Array {
37+
const values = new Uint8Array(40000)
38+
crypto.getRandomValues(values)
39+
return values
40+
}
41+
42+
export function copyArrayBuffer(bytes: Uint8Array): Uint8Array {
43+
const result = new Uint8Array(bytes.length)
44+
for (let i = 0; i < bytes.length; i++) {
45+
result[i] = bytes[i]
46+
}
47+
return result
48+
}
49+
3650
export function areTheSame(
3751
array1: Array<number | string | boolean | Uint8Array>,
3852
array2: Array<number | string | boolean | Uint8Array>

0 commit comments

Comments
 (0)