Skip to content

Commit

Permalink
test-socket-sender-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gooverdian committed Sep 16, 2021
1 parent 33164f2 commit b73329c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
26 changes: 23 additions & 3 deletions lib/routes.js
Expand Up @@ -29,15 +29,29 @@ const setupRoutes = function setupRoutes() {
const events = ['build', 'done', 'invalid', 'progress'];
const connect = async (ctx) => {
if (ctx.ws) {
console.log('/wps handler');
const socket = await ctx.ws();

const pingTimer = setInterval(() => socket.ping(), 30000);
const pingTimer = setInterval(() => {
try {
socket.ping();
} catch (error) {
console.log('socket ping error', error);
}
}, 30000);

const send = (data) => {
console.log('sending', data);
if (socket.readyState !== 1) {
return;
}
socket.send(data);

try {
socket.send(data);
} catch (error) {
console.log('socket send error', error);
}
console.log('sent', data);
};

socket.build = (compilerName = '<unknown>', { wpsId }) => {
Expand Down Expand Up @@ -83,7 +97,8 @@ const setupRoutes = function setupRoutes() {

socket.invalid = (filePath = '<unknown>', compiler) => {
const context = compiler.context || compiler.options.context || process.cwd();
const fileName = (filePath && filePath.replace && filePath.replace(context, '')) || filePath;
const fileName =
(filePath && filePath.replace && filePath.replace(context, '')) || filePath;
const { wpsId } = compiler;

send(prep({ action: 'invalid', data: { fileName, wpsId } }));
Expand All @@ -105,7 +120,12 @@ const setupRoutes = function setupRoutes() {
// onto the client via the socket
const unhandled = ({ eventName, data }) => send(prep({ action: eventName, data }));
this.on('unhandled', unhandled);
socket.on('error', (error) => {
console.trace();
console.error('socket error', error);
});
socket.on('close', () => {
console.log('socket closed');
clearInterval(pingTimer);
this.off('unhandled', unhandled);
});
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b73329c

Please sign in to comment.