-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
33 lines (27 loc) · 982 Bytes
/
app.ts
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
import WebSocket from "./node_modules/@types/ws";
import { Request, Response } from "./node_modules/@types/express";
const ws = require("ws");
const express = require("express");
const path = require("path");
require("dotenv").config();
const PORT = process.env.PORT || 3000;
const INDEX = "/index.html";
// Express server for static client
const server = express()
.use(express.static("static"))
.use((req: Request, res: Response) =>
res.sendFile(INDEX, { root: __dirname })
)
.listen(PORT, () => console.log(`Listening on ${PORT}`));
const wss = new ws.Server({ server });
wss.on("connection", function connection(client: WebSocket) {
// Client sends message to the server
client.on("message", function incoming(message: unknown) {
// ... server broadcasting the message to ALL connected clients
wss.clients.forEach(function each(client: WebSocket) {
if (client.readyState === ws.OPEN) {
client.send(message);
}
});
});
});