/
open.js
48 lines (37 loc) · 1.08 KB
/
open.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
var emit = require("./utils/emit")
, DataChannel = require("./dataChannel")
module.exports = open
function open(connection) {
var configuration = connection._configuration
, stream = configuration.stream
, mdm = configuration.mdm
, ps = configuration.ps
stream.setHeader("remote", connection.remoteDescription)
stream.setHeader("local", connection.localDescription)
stream.writeHead()
stream.on("header", function (msg) {
if (msg && msg.open) {
onConnect()
}
})
mdm.on("connection", onConnection)
ps.pipe(stream).pipe(mdm)
ps.resume()
stream.on("end", onEnd)
function onConnect() {
connection.readyState = "active"
emit(connection, "statechange")
emit(connection, "open")
}
function onEnd() {
connection.readyState = "closed"
emit(connection, "statechange")
}
function onConnection(stream) {
emit(connection, "datachannel", {
channel: new DataChannel(stream, {
remote: true
})
})
}
}