Skip to content

Commit f6b7a86

Browse files
committed
Add websocket example and tests
1 parent 5e2872d commit f6b7a86

File tree

11 files changed

+3589
-2
lines changed

11 files changed

+3589
-2
lines changed

fixtures/helpers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export class WranglerDevRunner {
6262
return this.urlPromise;
6363
}
6464

65-
async stop(containerId: string[]): Promise<void> {
66-
for (const id of containerId) {
65+
async stop(containerId?: string[]): Promise<void> {
66+
for (const id of containerId ?? []) {
6767
await fetch(this.url + '/stop?id=' + id);
6868
}
6969
// give it a second to run the onStop hook before we kill the process

fixtures/websocket/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# syntax=docker/dockerfile:1
2+
FROM node:22-alpine
3+
4+
WORKDIR /usr/src/app
5+
6+
COPY container_src/package.json package.json
7+
RUN npm install
8+
9+
COPY container_src/server.js server.js
10+
11+
EXPOSE 8080
12+
STOPSIGNAL SIGINT
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"ws": "^8.0.0"
5+
}
6+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { createServer } from "http";
2+
import { WebSocketServer } from 'ws';
3+
4+
const server = createServer(function (req, res) {
5+
if (req.url === '/error') {
6+
res.writeHead(500, { "Content-Type": "text/plain" });
7+
res.end("Internal server error");
8+
return;
9+
}
10+
11+
res.writeHead(200, { "Content-Type": "text/plain" });
12+
res.end(`Hello from WebSocket test container! process.env.MESSAGE: ${process.env.MESSAGE}`);
13+
});
14+
15+
// Create WebSocket server
16+
const wss = new WebSocketServer({ server, path: '/ws' });
17+
18+
wss.on('connection', function connection(ws, req) {
19+
console.log('WebSocket connection established');
20+
21+
ws.on('message', function message(data) {
22+
console.log('WebSocket received:', data.toString());
23+
// Echo the message back with container info
24+
ws.send(`Echo from container: ${data.toString()} | MESSAGE: ${process.env.MESSAGE}`);
25+
});
26+
27+
ws.on('close', function close() {
28+
console.log('WebSocket connection closed');
29+
});
30+
31+
// Send welcome message
32+
ws.send(`WebSocket connected to container! MESSAGE: ${process.env.MESSAGE}`);
33+
});
34+
35+
server.listen(8080, function () {
36+
console.log(`WebSocket test server listening on port 8080`);
37+
});
38+
39+
server.on("exit", () => {
40+
console.log("WebSocket test server exiting");
41+
})
42+

0 commit comments

Comments
 (0)