Files
chrome-extensions-samples/functional-samples/tutorial.websockets/service-worker.js
Sebastian Benz 2852c64790 Add WebSocket sample (#976)
* Add WebSocket sample

This sample demonstrates how to use a websocket in a service worker.

* add minimum chrome version

* Address comments

* add more detail to the readme
* make sure glitch instance is booted

* fix spelling
2023-07-05 13:18:27 +02:00

58 lines
1.4 KiB
JavaScript

const TEN_SECONDS_MS = 10 * 1000;
let webSocket = null;
// Make sure the Glitch demo server is running
fetch('https://chrome-extension-websockets.glitch.me/', { mode: 'no-cors' });
// Toggle WebSocket connection on action button click
// Send a message every 10 seconds, the ServiceWorker will
// be kept alive as long as messages are being sent.
chrome.action.onClicked.addListener(async () => {
if (webSocket) {
disconnect();
} else {
connect();
keepAlive();
}
});
function connect() {
webSocket = new WebSocket('wss://chrome-extension-websockets.glitch.me/ws');
webSocket.onopen = (event) => {
chrome.action.setIcon({ path: 'icons/socket-active.png' });
};
webSocket.onmessage = (event) => {
console.log(event.data);
};
webSocket.onclose = (event) => {
chrome.action.setIcon({ path: 'icons/socket-inactive.png' });
console.log('websocket connection closed');
webSocket = null;
};
}
function disconnect() {
if (webSocket) {
webSocket.close();
}
}
function keepAlive() {
const keepAliveIntervalId = setInterval(
() => {
if (webSocket) {
console.log('ping');
webSocket.send('ping');
} else {
clearInterval(keepAliveIntervalId);
}
},
// It's important to pick an interval that's shorter than 30s, to
// avoid that the service worker becomes inactive.
TEN_SECONDS_MS
);
}