-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (55 loc) · 1.47 KB
/
index.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import express from "express";
import http from "http";
import { Server } from "socket.io";
import dotenv from "dotenv";
import sql from "mssql";
dotenv.config();
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const config = {
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
server: process.env.DB_SERVER,
database: process.env.DB_NAME,
options: {
encrypt: true,
},
};
const users = {};
app.use(express.static("public"));
app.get("/", (req, res) => {
res.sendFile("/public/index.html");
});
io.on("connection", (socket) => {
socket.on("new user", async (username) => {
// Insert the new user into the database
try {
const pool = await sql.connect(config);
await pool
.request()
.query(`INSERT INTO Users (Username) VALUES ('${username}')`);
} catch (err) {
console.error("Error inserting user into database: ", err);
}
users[socket.id] = username;
io.emit("user joined", username);
});
socket.on("chat message", (msg) => {
io.emit("chat message", { user: users[socket.id], message: msg });
});
socket.on("disconnect", () => {
if (users[socket.id]) {
const username = users[socket.id];
io.emit("user left", username);
delete users[socket.id];
}
});
});
// io.emit("some event", {
// someProperty: "some value",
// otherProperty: "other value",
// });
server.listen(3001, () => {
console.log("listening on 3001");
});