Loading

T9 APIs · Lesson 7

Games: WebSockets

Choose the right option, fill in the blanks, write small pieces of code and fix broken ones. Code runs in a safe box inside this page.

Solved 0 of 48 items in this lesson.

1 / 24

This program is a WebSocket server and its own client, run in the shop's folder. What does it print?

cat > app.js <<'END'
const { WebSocketServer, WebSocket: ServerSocket } = require("ws");
const wss = new WebSocketServer({ port: 3000 });
wss.on("connection", (socket) => socket.on("message", (data) => socket.send("echo: " + data)));
const ws = new WebSocket("ws://localhost:3000");
ws.onopen = () => ws.send("hi");
ws.onmessage = (event) => { console.log(event.data); ws.close(); };
ws.onclose = () => wss.close();
END
node app.js

Back to the lesson