-
Notifications
You must be signed in to change notification settings - Fork 8
/
broadcasting.js
202 lines (159 loc) · 6 KB
/
broadcasting.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
190
191
192
193
194
195
196
197
198
199
200
201
202
export const DFWS_URL = "https://webservices.james-livesey.repl.co";
export const DFWS_PING_DELAY = 3_000;
export var broadcasts = [];
export function waitForDfws() {
console.log("Pinging DFWS server...");
return fetch(DFWS_URL).then(function(response) {
if (response.status == 202) {
console.log("DFWS server is waking; will retry soon");
return new Promise(function(resolve, reject) {
setTimeout(function() {
waitForDfws().then(resolve).catch(reject);
}, DFWS_PING_DELAY);
});
}
return response.json();
}).then(function(data) {
if (data?.status == "ok") {
console.log("DFWS server is active");
return Promise.resolve();
}
return Promise.reject(data);
}).catch(function(error) {
console.warn(error);
// Might be CORS which is the issue; try pinging again until it works
return new Promise(function(resolve, reject) {
setTimeout(function() {
waitForDfws().then(resolve).catch(reject);
}, DFWS_PING_DELAY);
});
});
}
export function channelToId(channel) {
return waitForDfws().then(function() {
return fetch(`${DFWS_URL}/broadcasting/${encodeURI(channel)}`);
}).then(function(response) {
return response.json();
}).then(function(data) {
if (data?.status == "ok") {
return Promise.resolve(data?.id);
}
return Promise.reject(data);
});
}
export function createChannel(channel, id) {
return waitForDfws().then(function() {
return fetch(`${DFWS_URL}/broadcasting/${encodeURI(channel)}?id=${encodeURIComponent(id)}`, {method: "POST"});
}).then(function(response) {
return response.json();
}).then(function(data) {
if (data?.status == "ok") {
return Promise.resolve();
}
return Promise.reject(data);
});
}
export class Broadcast {
constructor() {
var thisScope = this;
this.peer = null;
this.isConnected = false;
this.isHost = false;
this.joinedConnections = [];
this.hostConnection = null;
this.dataCallbacks = [];
this.dataBuffer = [];
this.onData(function(data) {
thisScope.dataBuffer.push(data);
});
broadcasts.push(this);
}
host(channelName = null) {
var thisScope = this;
this.isHost = true;
thisScope.isConnected = true;
if (channelName == null) {
channelName = String(Math.round(Math.random() * 1e8)).padStart(8, "0");
}
return new Promise(function(resolve, reject) {
thisScope.peer = new Peer(null, {debug: 2});
thisScope.peer.on("open", function(id) {
createChannel(channelName, id).then(function() {
resolve(channelName);
}).catch(reject);
});
thisScope.peer.on("connection", function(connection) {
console.log("New connection:", connection.peer);
thisScope.joinedConnections.push(connection);
connection.on("close", function() {
thisScope.joinedConnections = thisScope.joinedConnections.filter((connectionItem) => connectionItem == connection);
});
connection.on("data", function(data) {
thisScope.dataCallbacks.forEach((callback) => callback(data));
thisScope.joinedConnections.forEach(function(otherConnection) {
if (otherConnection == connection) {
return;
}
otherConnection.send({...data, fromHost: false});
});
});
});
thisScope.peer.on("disconnected", function() {
thisScope.isConnected = false;
thisScope.isHost = false;
thisScope.joinedConnections = [];
});
});
}
join(channel) {
var thisScope = this;
this.isHost = false;
return channelToId(channel).then(function(id) {
return new Promise(function(resolve, reject) {
thisScope.peer = new Peer();
thisScope.peer.on("open", function() {
thisScope.hostConnection = thisScope.peer.connect(id, {reliable: true});
thisScope.hostConnection.on("open", function() {
console.log("Opened connection:", thisScope.hostConnection.peer);
resolve();
});
thisScope.hostConnection.on("data", function(data) {
thisScope.dataCallbacks.forEach((callback) => callback(data));
});
thisScope.peer.on("error", function(error) {
reject(error);
});
thisScope.peer.on("disconnected", function() {
thisScope.isConnected = false;
thisScope.hostConnection = null;
});
thisScope.hostConnection.on("close", function() {
thisScope.isConnected = false;
thisScope.hostConnection = null;
});
});
});
});
}
onData(callback) {
this.dataCallbacks.push(callback);
}
sendData(data) {
if (this.isHost) {
this.joinedConnections.forEach((connection) => connection.send({
data,
isHost: true,
timestamp: Date.now()
}));
return;
}
this.hostConnection?.send({
data,
isHost: false,
timestamp: Date.now()
});
}
readBuffer() {
return this.dataBuffer.shift() ?? null;
}
}