TF-3157 Update web socket with background service worker
TF-3157 Stub BroadcastChannel for mobile build
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
importScripts('flutter_service_worker.js?v={{flutter_service_worker_version}}');
|
||||
|
||||
var webSocket;
|
||||
const broadcast = new BroadcastChannel("background-message");
|
||||
var intervalId;
|
||||
const pingIntervalInMs = 10000;
|
||||
|
||||
function connect(url, ticket) {
|
||||
webSocket = new WebSocket(`${url}?ticket=${ticket}`, "jmap");
|
||||
|
||||
webSocket.onopen = () => {
|
||||
console.log("websocket open");
|
||||
webSocket.send(
|
||||
JSON.stringify({
|
||||
"@type": "WebSocketPushEnable",
|
||||
dataTypes: ["Mailbox", "Email"],
|
||||
})
|
||||
);
|
||||
intervalId = setInterval(() => {
|
||||
webSocket.send(
|
||||
JSON.stringify({
|
||||
"@type": "Request",
|
||||
id: "R1",
|
||||
using: ["urn:ietf:params:jmap:core"],
|
||||
methodCalls: [["Core/echo", {}, "c0"]],
|
||||
})
|
||||
);
|
||||
}, pingIntervalInMs);
|
||||
};
|
||||
|
||||
webSocket.onmessage = (event) => {
|
||||
console.log(`websocket received message: ${event.data}`);
|
||||
broadcast.postMessage(event.data);
|
||||
};
|
||||
|
||||
webSocket.onclose = (event) => {
|
||||
console.log(
|
||||
`websocket connection closed with code: "${event.code}" reason: "${event.reason}" and cleanly: "${event.wasClean}"`
|
||||
);
|
||||
broadcast.postMessage("webSocketClosed");
|
||||
webSocket = null;
|
||||
clearInterval(intervalId);
|
||||
};
|
||||
}
|
||||
|
||||
function disconnect() {
|
||||
if (webSocket == null) {
|
||||
return;
|
||||
}
|
||||
webSocket.close();
|
||||
}
|
||||
|
||||
self.addEventListener("install", (event) => {
|
||||
self.skipWaiting().then(() => {
|
||||
console.log("Service worker installed");
|
||||
});
|
||||
});
|
||||
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
self.clients.claim().then(() => {
|
||||
console.log("Service worker activated");
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener("message", (event) => {
|
||||
console.log(`web socket worker received message: ${event.data}`);
|
||||
if (event.data.action === "connect") {
|
||||
connect(event.data.url, event.data.ticket);
|
||||
} else if (event.data.action === "disconnect") {
|
||||
disconnect();
|
||||
}
|
||||
});
|
||||
@@ -4,7 +4,6 @@ const iosPlatform = 'iOS';
|
||||
const androidPlatform = 'android';
|
||||
const otherPlatform = 'other';
|
||||
const timeoutDuration = 4000;
|
||||
var serviceWorkerVersion = null;
|
||||
var scriptLoaded = false;
|
||||
|
||||
function loadMainDartJs() {
|
||||
@@ -24,16 +23,7 @@ function fetchServiceWorker() {
|
||||
// Wait for registration to finish before dropping the <script>tag.
|
||||
// Otherwise, the browser will load the script multiple times,
|
||||
// potentially different versions.
|
||||
navigator.serviceWorker.register('firebase-messaging-sw.js').then(serviceWorkerRegistration => {
|
||||
console.info('[TwakeMail] fetchServiceWorker(): Service worker firebase-messaging was registered.');
|
||||
}).catch(error => {
|
||||
console.error(
|
||||
'[TwakeMail] fetchServiceWorker(): An error occurred while registering the service worker firebase-messaging.'
|
||||
);
|
||||
console.error(error);
|
||||
});
|
||||
var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion;
|
||||
navigator.serviceWorker.register(serviceWorkerUrl)
|
||||
navigator.serviceWorker.register('web-sockets-worker.js')
|
||||
.then((reg) => {
|
||||
function waitForActivation(serviceWorker) {
|
||||
serviceWorker.addEventListener('statechange', () => {
|
||||
@@ -47,12 +37,6 @@ function fetchServiceWorker() {
|
||||
// No active web worker and we have installed or are installing
|
||||
// one for the first time. Simply wait for it to activate.
|
||||
waitForActivation(reg.installing || reg.waiting);
|
||||
} else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
|
||||
// When the app updates the serviceWorkerVersion changes, so we
|
||||
// need to ask the service worker to update.
|
||||
console.log('[TwakeMail] fetchServiceWorker(): New service worker available.');
|
||||
reg.update();
|
||||
waitForActivation(reg.installing);
|
||||
} else {
|
||||
// Existing service worker is still good.
|
||||
console.log('[TwakeMail] fetchServiceWorker(): Loading app from service worker.');
|
||||
|
||||
Reference in New Issue
Block a user