Skip to content

Commit

Permalink
Improved ws error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed Mar 19, 2021
1 parent f3d691f commit 6fa9ac5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kras",
"version": "0.12.1",
"version": "0.12.2",
"description": "Efficient server proxying and mocking in Node.js.",
"main": "dist/server/index.js",
"types": "dist/server/index.d.ts",
Expand Down
10 changes: 10 additions & 0 deletions src/server/core/webserver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as express from 'express';
import * as expressWs from 'express-ws';
import type { Server as WebSocketServer } from 'ws';
import { Application, Request, Response } from 'express';
import { createServer as createHttpServer, Server as HttpServer } from 'http';
import { createServer as createHttpsServer, Server as HttpsServer } from 'https';
Expand Down Expand Up @@ -143,6 +144,15 @@ export class WebServer extends EventEmitter implements BaseKrasServer {
this.sockets = expressWs(this.app, this.server, {
wsOptions: this.wsOptions,
});
const wsServer = (this.sockets.getWss() as WebSocketServer);
wsServer.on('connection', socket => {
socket.on('error', (err) => {
this.emit('error', `Problem with the WS socket connection: ${err}`);
});
});
wsServer.on('error', err => {
this.emit('error', `Error with WS server: ${err}`);
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/injectors/proxy-injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default class ProxyInjector implements KrasInjector {
rejectUnauthorized: false,
headers,
});
ws.on('error', err => core.emit('error', err));
ws.on('open', () => {
open = true;

Expand Down Expand Up @@ -136,7 +137,6 @@ export default class ProxyInjector implements KrasInjector {
});
}
});
ws.on('error', err => core.emit('error', err));
this.sessions[e.id] = ws;
}
});
Expand Down

0 comments on commit 6fa9ac5

Please sign in to comment.