샘플
ws 8.21.3: Run a WebSocket server and client round trip with ws
검증된 샘플 — npm ws 8.21.3: Run a WebSocket server and client round trip with ws. node 22 · linux alpine/x64 · docker에서 contract를 실행해 통과했습니다.
sha256:91b961d23ba84c3205b8885fc80b5203a7d47a923e7a8aab6729d513e2a66a95
이 네트워크가 제공하는 것은 하나입니다. 빌드되는 샘플. 샌드박스에서 돌리고 서명된 영수증을 보관합니다. 등급을 매기지 않고 무엇도 보증하지 않습니다 — 같은 코드가 당신 환경에서 빌드되는지는 측정한 적이 없습니다.
통과한 계약 영수증을 낸 서로 다른 서명 키의 수입니다. 하나면 작성자 혼자이고, 둘 이상이면 다른 사람도 빌드했다는 뜻입니다. 키는 스스로 만드는 것이고 뒤에 등록된 신원이 없으므로, 세는 것은 사람이 아니라 키입니다.
MIT-0
실행 증거
선언된 환경과 서명된 실행을 분리해 두었습니다. 이 샘플이 무엇을 어디서 실행했는지 그대로 볼 수 있습니다.
- 증거 기준
- 서명된 컨트랙트 통과
- 검증 영수증
- 3
- 빌드한 서명 키
- 3
선언된 환경
node 22 linux x64 node 22 npm
검증 실행 환경
| 환경 | 컨트랙트 | 단계 | 실행일 |
|---|---|---|---|
| node 22 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-13 |
| node 22 · linux alpine/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1 |
2026-08-17 |
| node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5… |
2026-09-07 |
케이스
HOW- 목표
- Run a WebSocket server and client round trip with ws
- 패키지
- 심벌
-
- WebSocketServer
- WebSocket
- 환경
- node 22
- 생성일
- 2026-08-13T15:20:48Z
컨트랙트
- start a WebSocketServer on an ephemeral port
- connect with a ws client and send a message
- assert the server received it and the echoed reply arrived
- assert both sides closed cleanly
파일
- csx.json
- package-lock.json
- package.json
- src/index.mjs
- test/contract.mjs
소스
{"case":{"caseId":"case:sha256:b14a37c3af8b097a19b4f33c378c82a108142c346747ca86fcd0104ba361203d","constraints":{"moduleSystem":"esm","runtime":"node"},"contract":["start a WebSocketServer on an ephemeral port","connect with a ws client and send a message","assert the server received it and the echoed reply arrived","assert both sides closed cleanly"],"goal":"Run a WebSocket server and client round trip with ws","kind":"HOW","packages":["pkg:npm/ws@8.21.3"],"schemaVersion":1,"symbols":["WebSocketServer","WebSocket"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","ecosystem":"npm","executionContext":"node","moduleSystem":"esm","os":"linux","packageManager":"npm","runtime":"node","runtimeVersion":"22","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/ws@8.21.3"],"schemaVersion":1,"symbols":["WebSocketServer","WebSocket"],"verifierAdapter":"node-typescript@1"}
{
"name": "csx-sample-ws-echo-roundtrip",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "csx-sample-ws-echo-roundtrip",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"ws": "8.21.3"
}
},
"node_modules/ws": {
"version": "8.21.3",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.21.3.tgz",
"integrity": "sha512-201TZ/kPWxoPr/OKWjquZR1SWKXcvxdH+e1xrx89b3YbmzLMFCLfnaG1HFIgWzJOEWZ7MvpK++odZufgYR50Rw==",
"license": "MIT",
"engines": {
"node": ">=10.0.0"
},
"peerDependencies": {
"bufferutil": "^4.0.1",
"utf-8-validate": ">=5.0.2"
},
"peerDependenciesMeta": {
"bufferutil": {
"optional": true
},
"utf-8-validate": {
"optional": true
}
}
}
}
}
{
"name": "csx-sample-ws-echo-roundtrip",
"version": "1.0.0",
"private": true,
"type": "module",
"license": "MIT-0",
"dependencies": {
"ws": "8.21.3"
}
}
import { WebSocketServer, WebSocket } from 'ws';
// ws is CommonJS but ships named exports that ESM can import directly.
// Messages arrive as Buffer by default, so compare with String(data) rather
// than === on the raw value.
export function startEchoServer() {
const server = new WebSocketServer({ port: 0, host: '127.0.0.1' });
server.on('connection', (socket) => {
socket.on('message', (data) => socket.send(`echo:${String(data)}`));
});
return new Promise((resolve) => server.on('listening', () => resolve(server)));
}
export function askEcho(port, message) {
return new Promise((resolve, reject) => {
const client = new WebSocket(`ws://127.0.0.1:${port}`);
client.on('open', () => client.send(message));
client.on('message', (data) => {
client.close();
resolve(String(data));
});
client.on('error', reject);
});
}
import { strict as assert } from 'node:assert';
import { startEchoServer, askEcho } from '../src/index.mjs';
const server = await startEchoServer();
const { port } = server.address();
const reply = await askEcho(port, 'hello');
assert.equal(reply, 'echo:hello');
await new Promise((r) => server.close(r));
console.log('CONTRACT PASS: ws server and client completed an echo round trip');