Bug 1607074 - Fetch websocket address from cloud server address, r=jlast.

Differential Revision: https://phabricator.services.mozilla.com/D58719

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Brian Hackett 2020-01-07 00:00:54 +00:00
Родитель 3c0275f1d5
Коммит 041540c18f
1 изменённых файлов: 25 добавлений и 19 удалений

Просмотреть файл

@ -36,23 +36,30 @@ self.addEventListener("message", function({ data }) {
const gConnections = []; const gConnections = [];
function doConnect(id, channelId, address) { async function doConnect(id, channelId, address) {
if (gConnections[id]) { if (gConnections[id]) {
ThrowError(`Duplicate connection ID ${id}`); ThrowError(`Duplicate connection ID ${id}`);
} }
const socket = new WebSocket(address); const connection = { outgoing: [] };
gConnections[id] = connection;
// The cloud server address we are given provides us with the address of a
// websocket server we should connect to.
const response = await fetch(address);
const text = await response.text();
if (!/^wss?:\/\//.test(text)) {
ThrowError(`Invalid websocket address ${text}`);
}
const socket = new WebSocket(text);
socket.onopen = evt => onOpen(id, evt); socket.onopen = evt => onOpen(id, evt);
socket.onclose = evt => onClose(id, evt); socket.onclose = evt => onClose(id, evt);
socket.onmessage = evt => onMessage(id, evt); socket.onmessage = evt => onMessage(id, evt);
socket.onerror = evt => onError(id, evt); socket.onerror = evt => onError(id, evt);
const connection = { outgoing: [] }; await new Promise(resolve => (connection.openWaiter = resolve));
const promise = new Promise(r => (connection.openWaiter = r));
gConnections[id] = connection;
(async function sendMessages() {
await promise;
while (gConnections[id]) { while (gConnections[id]) {
if (connection.outgoing.length) { if (connection.outgoing.length) {
const buf = connection.outgoing.shift(); const buf = connection.outgoing.shift();
@ -65,7 +72,6 @@ function doConnect(id, channelId, address) {
await new Promise(resolve => (connection.sendWaiter = resolve)); await new Promise(resolve => (connection.sendWaiter = resolve));
} }
} }
})();
} }
function doSend(id, buf) { function doSend(id, buf) {