forked from feross/simple-peer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary.js
122 lines (103 loc) 路 3.59 KB
/
binary.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
117
118
119
120
121
122
var common = require('./common')
var Peer = require('../')
var test = require('tape')
var config
test('get config', function (t) {
common.getConfig(function (err, _config) {
if (err) return t.fail(err)
config = _config
t.end()
})
})
test('data send/receive Buffer', function (t) {
t.plan(6)
var peer1 = new Peer({ config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config, wrtc: common.wrtc })
peer1.on('signal', function (data) {
peer2.signal(data)
})
peer2.on('signal', function (data) {
peer1.signal(data)
})
peer1.on('connect', tryTest)
peer2.on('connect', tryTest)
function tryTest () {
if (!peer1.connected || !peer2.connected) return
peer1.send(Buffer.from([0, 1, 2]))
peer2.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')
peer2.send(Buffer.from([0, 2, 4]))
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 2, 4]), 'got correct message')
peer1.on('close', function () { t.pass('peer1 destroyed') })
peer1.destroy()
peer2.on('close', function () { t.pass('peer2 destroyed') })
peer2.destroy()
})
})
}
})
test('data send/receive Uint8Array', function (t) {
t.plan(6)
var peer1 = new Peer({ config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config, wrtc: common.wrtc })
peer1.on('signal', function (data) {
peer2.signal(data)
})
peer2.on('signal', function (data) {
peer1.signal(data)
})
peer1.on('connect', tryTest)
peer2.on('connect', tryTest)
function tryTest () {
if (!peer1.connected || !peer2.connected) return
peer1.send(new Uint8Array([0, 1, 2]))
peer2.on('data', function (data) {
// binary types always get converted to Buffer
// See: https://github.com/feross/simple-peer/issues/138#issuecomment-278240571
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')
peer2.send(new Uint8Array([0, 2, 4]))
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 2, 4]), 'got correct message')
peer1.on('close', function () { t.pass('peer1 destroyed') })
peer1.destroy()
peer2.on('close', function () { t.pass('peer2 destroyed') })
peer2.destroy()
})
})
}
})
test('data send/receive ArrayBuffer', function (t) {
t.plan(6)
var peer1 = new Peer({ config, initiator: true, wrtc: common.wrtc })
var peer2 = new Peer({ config, wrtc: common.wrtc })
peer1.on('signal', function (data) {
peer2.signal(data)
})
peer2.on('signal', function (data) {
peer1.signal(data)
})
peer1.on('connect', tryTest)
peer2.on('connect', tryTest)
function tryTest () {
if (!peer1.connected || !peer2.connected) return
peer1.send(new Uint8Array([0, 1, 2]).buffer)
peer2.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 1, 2]), 'got correct message')
peer2.send(new Uint8Array([0, 2, 4]).buffer)
peer1.on('data', function (data) {
t.ok(Buffer.isBuffer(data), 'data is Buffer')
t.deepEqual(data, Buffer.from([0, 2, 4]), 'got correct message')
peer1.on('close', function () { t.pass('peer1 destroyed') })
peer1.destroy()
peer2.on('close', function () { t.pass('peer2 destroyed') })
peer2.destroy()
})
})
}
})