-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-app.mjs
More file actions
49 lines (39 loc) · 1.25 KB
/
chat-app.mjs
File metadata and controls
49 lines (39 loc) · 1.25 KB
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
39
40
41
42
43
44
45
46
47
48
49
/** Web- and Socket Service for Chat-App
*
* @package Webapplication
* @module Chat
* @author Michael <michael.reichart@gfu.net>
* @version v1.0.0
* @since 2024-09-11
* @see i.e. inspired by ... {link to}
* @license MIT {https://opensource.org/licenses/MIT}
* @copyright (c) 2024 Michael Reichart, Cologne
*/
import http from "http";
import express from "express";
import { Server } from "socket.io";
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const host = "localhost";
const port = 8001;
app.use(express.static("static"));
app.get("/", (request, response) => {
response.sendFile("/static/templates/chat.html", { root: "./" });
});
io.on("connection", (socket) => {
console.log("New connection established");
socket.on("disconnect", () => {
console.log("Connection closed");
});
socket.on("chat message", (message) => {
console.log("New message received: ", message);
// Nachricht an alle Clients ausliefern
socket.emit("chat message", message);
socket.broadcast.emit("chat message", message);
// io.emit("chat message", message);
});
});
server.listen(port, host, () => {
console.log(`Server is running at http://${host}:${port}`);
});