Skip to content

Commit

Permalink
refactor: rewite client side script and supress server side logs
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Apr 9, 2024
1 parent 7e6910b commit 257068f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ hexo.extend.injector.register('body_end', () => {
const config = getOrDefault(hexo);
// TODO: error handling (Websocket connection failed. etc...) & retry.
// TODO: host should be configurable?
return '<script>'
+ `const socket = new WebSocket("ws://localhost:${config.server.port}");`
+ 'const path = window.location.pathname.split("?")[0];'
+ 'const sendReloadingMessage = () => { socket.send("The browser is reloading. Please wait for the reload. This take a little longer if there are a lot of routes (post, page, assets, tags, categories etc).") };'
+ 'socket.addEventListener("open", (event) => {socket.send("Connection established from " + path)});'
+ `socket.addEventListener("message", (event) => {if (event.data === "${config.notification.message}") { sendReloadingMessage(); socket.close(); location.reload();}});`
+ '</script>';
const s = `
const socket = new WebSocket("ws://localhost:${config.server.port}");
const path = window.location.pathname.split("?")[0];
const reloadingMessage = '{"type":"reload", "message": "The browser is reloading. Please wait for the reload. This take a little longer if there are a lot of routes (post, page, assets, tags, categories etc)."}';
const connectedMessage = JSON.stringify({"type":"connected", "message": "Connection established from " + path});
const sendReloadingMessage = () => { socket.send(reloadingMessage) };
socket.addEventListener("open", (event) => {socket.send(connectedMessage)});
socket.addEventListener("message", (event) => {if (event.data === "${config.notification.message}") { sendReloadingMessage(); socket.close(); location.reload();}});
`;

return `<script>${s}</script>`;
}
});
17 changes: 14 additions & 3 deletions src/webSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const createWebSocketServer = (config: Config) => {
port: config.server.port
});

log.info(`${p} plugin config is: \n ${JSON.stringify(config, null, 2)}`);
log.debug(`${p} plugin config is: \n ${JSON.stringify(config, null, 2)}`);

wss.on('error', err => {
log.error(`${p} ${err}`);
Expand All @@ -24,11 +24,22 @@ export const createWebSocketServer = (config: Config) => {
log.debug(`${p} Connection established.`);

ws.on('message', data => {
log.info(`${p} ${data}`);
const d = JSON.parse(data);

switch (d.type) {
case 'connected':
log.debug(`${p} ${d.message}`);
break;
case 'reload':
log.info(`${p} ${d.message}`);
break;
default:
// Nothing todo
}
});

ws.on('close', () => {
log.info(`${p} Connection closed`);
log.debug(`${p} Connection closed`);
});

ws.on('error', err => {
Expand Down

0 comments on commit 257068f

Please sign in to comment.