Fix server crashing when sending data on closing/closed socket
This commit is contained in:
parent
4e6d6f0725
commit
4eb9f87217
2 changed files with 12 additions and 7 deletions
|
@ -117,11 +117,9 @@ export class Entry extends Command {
|
||||||
app.wss.on("connection", (ws, req) => {
|
app.wss.on("connection", (ws, req) => {
|
||||||
const id = clientId++;
|
const id = clientId++;
|
||||||
|
|
||||||
ws.on("open", () => {
|
|
||||||
if (sharedProcess.state === SharedProcessState.Ready) {
|
if (sharedProcess.state === SharedProcessState.Ready) {
|
||||||
sendSharedProcessReady(ws);
|
sendSharedProcessReady(ws);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
logger.info(`WebSocket opened \u001B[0m${req.url}`, field("client", id), field("ip", req.socket.remoteAddress));
|
logger.info(`WebSocket opened \u001B[0m${req.url}`, field("client", id), field("ip", req.socket.remoteAddress));
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import { field, logger } from "@coder/logger";
|
||||||
import { ReadWriteConnection } from "@coder/protocol";
|
import { ReadWriteConnection } from "@coder/protocol";
|
||||||
import { Server, ServerOptions } from "@coder/protocol/src/node/server";
|
import { Server, ServerOptions } from "@coder/protocol/src/node/server";
|
||||||
import { NewSessionMessage } from '@coder/protocol/src/proto';
|
import { NewSessionMessage } from '@coder/protocol/src/proto';
|
||||||
|
@ -24,13 +25,19 @@ export const createApp = (registerMiddleware?: (app: express.Application) => voi
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
wss.on("connection", (ws: WebSocket, req) => {
|
wss.on("connection", (ws) => {
|
||||||
const connection: ReadWriteConnection = {
|
const connection: ReadWriteConnection = {
|
||||||
onMessage: (cb): void => {
|
onMessage: (cb): void => {
|
||||||
ws.addEventListener("message", (event) => cb(event.data));
|
ws.addEventListener("message", (event) => cb(event.data));
|
||||||
},
|
},
|
||||||
close: (): void => ws.close(),
|
close: (): void => ws.close(),
|
||||||
send: (data): void => ws.send(data),
|
send: (data): void => {
|
||||||
|
try {
|
||||||
|
ws.send(data);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(error.message, field("error", error));
|
||||||
|
}
|
||||||
|
},
|
||||||
onClose: (cb): void => ws.addEventListener("close", () => cb()),
|
onClose: (cb): void => ws.addEventListener("close", () => cb()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue