mirror of
https://github.com/fregante/GhostText.git
synced 2024-11-24 12:15:54 +03:00
Avoid auto-disconnect when inactive (#318)
This commit is contained in:
parent
3fb653eea1
commit
745604460d
@ -97,12 +97,17 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
|
||||
|
||||
console.log('will open socket');
|
||||
const socket = new WebSocket('ws://localhost:' + WebSocketPort);
|
||||
let interval; // eslint-disable-line prefer-const -- Ur rong
|
||||
await Promise.race([
|
||||
oneEvent(socket, 'open'),
|
||||
oneEvent(socket, 'error'),
|
||||
]);
|
||||
|
||||
const onSocketClose = () => port.postMessage({close: true});
|
||||
const onSocketClose = () => {
|
||||
port.postMessage({close: true});
|
||||
clearInterval(interval);
|
||||
};
|
||||
|
||||
socket.addEventListener('close', onSocketClose);
|
||||
socket.addEventListener('message', event => port.postMessage({message: event.data}));
|
||||
socket.addEventListener('error', event => console.error('error!', event));
|
||||
@ -114,8 +119,15 @@ chrome.runtime.onConnect.addListener(handlePortListenerErrors(async port => {
|
||||
port.onDisconnect.addListener(() => {
|
||||
socket.removeEventListener('close', onSocketClose);
|
||||
socket.close();
|
||||
clearInterval(interval);
|
||||
});
|
||||
port.postMessage({ready: true});
|
||||
|
||||
interval = setInterval(() => {
|
||||
// Keep-alive for MV3 https://github.com/fregante/GhostText/issues/317
|
||||
port.postMessage({ping: true});
|
||||
console.log('ping');
|
||||
}, 5000);
|
||||
}));
|
||||
|
||||
function handleMessages({code, count}, {tab}) {
|
||||
|
Loading…
Reference in New Issue
Block a user