-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (33 loc) · 985 Bytes
/
Copy pathindex.js
File metadata and controls
38 lines (33 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const express = require("express");
const path = require("path");
const WebSocket = require("ws");
const app = express();
// Use the public directory for static file requests
app.use(express.static("public"));
// Start our WS server at 3001
const wss = new WebSocket.Server({ port: 3001 });
wss.on("connection", ws => {
console.log("A new client connected!");
ws.on("message", data => {
console.log(`Message from client: ${data}`);
const parsed = JSON.parse(data);
ws.send(
JSON.stringify({
...parsed.data,
messageFromServer: `Hello tab id: ${parsed.data.from}`
})
);
});
ws.on("close", () => {
console.log("Sad to see you go :(");
});
setTimeout(
() =>
ws.send(JSON.stringify({ broadcast: "A broadcast for all clients!" })),
15000
);
});
// Listen for requests for static pages at 3000
const server = app.listen(3000, function() {
console.log("The server is running on http://localhost:" + 3000);
});