Skip to content

Commit 3d0f804

Browse files
[INS-1693] Add WS echo server for smoke tests (#5050)
Co-authored-by: Dimitri Mitropoulos <[email protected]>
1 parent 48bbfa9 commit 3d0f804

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

packages/insomnia-smoke-test/package-lock.json

+48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/insomnia-smoke-test/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"@types/oidc-provider": "^7.8.1",
4242
"@types/ramda": "^0.27.45",
4343
"@types/uuid": "^8.3.4",
44+
"@types/ws": "^8.5.3",
4445
"concurrently": "^7.0.0",
4546
"cross-env": "^7.0.3",
4647
"execa": "^5.0.0",
@@ -57,6 +58,7 @@
5758
"ramda-adjunct": "^2.34.0",
5859
"ts-node": "^9.1.1",
5960
"uuid": "^8.3.2",
61+
"ws": "^8.8.1",
6062
"xvfb-maybe": "^0.2.1"
6163
}
6264
}

packages/insomnia-smoke-test/server/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import gitlabApi from './gitlab-api';
77
import { root, schema } from './graphql';
88
import { startGRPCServer } from './grpc';
99
import { oauthRoutes } from './oauth';
10+
import { startWebsocketServer } from './websocket';
1011

1112
const app = express();
1213
const port = 4010;
@@ -55,7 +56,9 @@ app.use('/graphql', graphqlHTTP({
5556
}));
5657

5758
startGRPCServer(grpcPort).then(() => {
58-
app.listen(port, () => {
59+
const server = app.listen(port, () => {
5960
console.log(`Listening at http://localhost:${port}`);
61+
console.log(`Listening at ws://localhost:${port}`);
6062
});
63+
startWebsocketServer(server);
6164
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Server } from 'http';
2+
import { WebSocketServer } from 'ws';
3+
4+
/**
5+
* Starts an echo WebSocket server that receives messages from a client and echoes them back.
6+
*/
7+
export function startWebsocketServer(server: Server) {
8+
const wsServer = new WebSocketServer({ server });
9+
10+
wsServer.on('connection', ws => {
11+
console.log('WebSocket connection was opened');
12+
13+
ws.on('message', (message, isBinary) => {
14+
if (isBinary) {
15+
ws.send(message);
16+
return;
17+
}
18+
19+
ws.send(message.toString());
20+
});
21+
22+
ws.on('close', () => {
23+
console.log('WebSocket connection was closed');
24+
});
25+
});
26+
27+
return wsServer;
28+
}

0 commit comments

Comments
 (0)