forked from cjb/serverless-webrtc
-
Notifications
You must be signed in to change notification settings - Fork 5
/
serverless-webrtc.js
189 lines (167 loc) · 5.25 KB
/
serverless-webrtc.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env node
// This script runs under node, and `serverless-webrtc.html` runs inside
// a browser.
// Usage: `node serverless-webrtc.js` or `node serverless-webrtc.js --create`.
var webrtc = require('wrtc');
var readline = require('readline');
var ansi = require('ansi');
var cursor = ansi(process.stdout);
var pc = null;
var offer = null;
var answer = null;
/* 1. Global settings, data and functions. */
var dataChannelSettings = {
'reliable': {
ordered: true,
maxRetransmits: 0
},
};
var pcSettings = [
{
iceServers: [{url:'stun:stun.l.google.com:19302'}]
},
{
'optional': [{DtlsSrtpKeyAgreement: false}]
}
];
var pendingDataChannels = {};
var dataChannels = {}
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
if (process.argv[2] == "--create") {
makeOffer();
}
else {
rl.question("Please paste your offer:\n", function(offer) {
getOffer(offer);
});
}
function doHandleError(error) {
throw error;
}
function onsignalingstatechange(state) {
//console.info('signaling state change:', state);
}
function oniceconnectionstatechange(state) {
//console.info('ice connection state change:', state);
}
function onicegatheringstatechange(state) {
//console.info('ice gathering state change:', state);
}
function inputLoop(channel) {
cursor.green();
rl.question("> ", function(text) {
channel.send(JSON.stringify({message: text}));
inputLoop(channel);
});
}
/* 2. This code deals with the --join case. */
function getOffer(pastedOffer) {
data = JSON.parse(pastedOffer);
offer = new webrtc.RTCSessionDescription(data);
answer = null;
pc = new webrtc.RTCPeerConnection(pcSettings);
pc.onsignalingstatechange = onsignalingstatechange;
pc.oniceconnectionstatechange = oniceconnectionstatechange;
pc.onicegatheringstatechange = onicegatheringstatechange;
pc.onicecandidate = function(candidate) {
// Firing this callback with a null candidate indicates that
// trickle ICE gathering has finished, and all the candidates
// are now present in pc.localDescription. Waiting until now
// to create the answer saves us from having to send offer +
// answer + iceCandidates separately.
if (candidate.candidate == null) {
doShowAnswer();
}
}
doHandleDataChannels();
}
function doShowAnswer() {
answer = pc.localDescription;
console.log("\n\nHere is your answer:");
console.log(JSON.stringify(answer) + "\n\n");
}
function doCreateAnswer() {
pc.createAnswer(doSetLocalDesc, doHandleError);
}
function doSetLocalDesc(desc) {
answer = desc;
pc.setLocalDescription(desc, undefined, doHandleError);
};
function doHandleDataChannels() {
var labels = Object.keys(dataChannelSettings);
pc.ondatachannel = function(evt) {
var channel = evt.channel;
var label = channel.label;
pendingDataChannels[label] = channel;
//channel.binaryType = 'arraybuffer';
channel.onopen = function() {
dataChannels[label] = channel;
delete pendingDataChannels[label];
if(Object.keys(dataChannels).length === labels.length) {
console.log("\nConnected!");
inputLoop(channel);
}
};
channel.onmessage = function(evt) {
data = JSON.parse(evt.data);
cursor.blue();
console.log(data.message);
inputLoop(channel);
};
channel.onerror = doHandleError;
};
pc.setRemoteDescription(offer, doCreateAnswer, doHandleError);
}
/* 3. From here on down deals with the --create case. */
function makeOffer() {
pc = new webrtc.RTCPeerConnection(pcSettings);
makeDataChannel();
pc.onsignalingstatechange = onsignalingstatechange;
pc.oniceconnectionstatechange = oniceconnectionstatechange;
pc.onicegatheringstatechange = onicegatheringstatechange;
pc.createOffer(function (desc) {
pc.setLocalDescription(desc, function () {});
// We'll pick up the offer text once trickle ICE is complete,
// in onicecandidate.
});
pc.onicecandidate = function(candidate) {
// Firing this callback with a null candidate indicates that
// trickle ICE gathering has finished, and all the candidates
// are now present in pc.localDescription. Waiting until now
// to create the answer saves us from having to send offer +
// answer + iceCandidates separately.
if (candidate.candidate == null) {
console.log("Your offer is:");
console.log(JSON.stringify(pc.localDescription));
rl.question("Please paste your answer:\n", function(answer) {
getAnswer(answer);
});
}
}
}
function makeDataChannel() {
// If you don't make a datachannel *before* making your offer (such
// that it's included in the offer), then when you try to make one
// afterwards it just stays in "connecting" state forever. This is
// my least favorite thing about the datachannel API.
var channel = pc.createDataChannel('test', {reliable:true});
channel.onopen = function() {
console.log("\nConnected!");
inputLoop(channel);
};
channel.onmessage = function(evt) {
data = JSON.parse(evt.data);
cursor.blue();
console.log(data.message);
inputLoop(channel);
};
channel.onerror = doHandleError;
}
function getAnswer(pastedAnswer) {
data = JSON.parse(pastedAnswer);
answer = new webrtc.RTCSessionDescription(data);
pc.setRemoteDescription(answer);
}