зеркало из https://github.com/mozilla/gecko-dev.git
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:
Родитель
3c0275f1d5
Коммит
041540c18f
|
@ -36,23 +36,30 @@ self.addEventListener("message", function({ data }) {
|
|||
|
||||
const gConnections = [];
|
||||
|
||||
function doConnect(id, channelId, address) {
|
||||
async function doConnect(id, channelId, address) {
|
||||
if (gConnections[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.onclose = evt => onClose(id, evt);
|
||||
socket.onmessage = evt => onMessage(id, evt);
|
||||
socket.onerror = evt => onError(id, evt);
|
||||
|
||||
const connection = { outgoing: [] };
|
||||
const promise = new Promise(r => (connection.openWaiter = r));
|
||||
await new Promise(resolve => (connection.openWaiter = resolve));
|
||||
|
||||
gConnections[id] = connection;
|
||||
|
||||
(async function sendMessages() {
|
||||
await promise;
|
||||
while (gConnections[id]) {
|
||||
if (connection.outgoing.length) {
|
||||
const buf = connection.outgoing.shift();
|
||||
|
@ -65,7 +72,6 @@ function doConnect(id, channelId, address) {
|
|||
await new Promise(resolve => (connection.sendWaiter = resolve));
|
||||
}
|
||||
}
|
||||
})();
|
||||
}
|
||||
|
||||
function doSend(id, buf) {
|
||||
|
|
Загрузка…
Ссылка в новой задаче