-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
63 lines (44 loc) · 1.44 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
const http = require("http")
const express = require("express")
const { Server } = require("socket.io")
const port = process.env.PORT || 3000
const app = express()
const httpServer = http.createServer(app)
const io = new Server(httpServer)
app.set("view engine", "ejs")
app.set("views", "views")
app.use(express.static("public"))
// Socket.io
io.on("connection", async (socket) => {
console.log("Client connected: ", socket.id)
const sockets = await io.fetchSockets()
// Tell new user about previously existing users
if (sockets.length > 1) {
socket.emit("user:dump", sockets.map(s => {
return {id: s.id, username: s.username}
}))
}
// Tell everyone else about new user
socket.on("user:enter", (data) => {
socket.username = data.username
socket.broadcast.emit("user:enter", data)
})
socket.on("disconnect", reason => {
console.log("Client disconnected: ", socket.id, `(${reason})`)
socket.broadcast.emit("user:leave", socket.id)
})
socket.on("groupMsg:post", (msg) => {
socket.broadcast.emit("groupMsg:get", msg)
})
socket.on("privMsg:post", (id, msg) => {
// Send message to user with id
socket.to(id).emit("privMsg:get", msg)
})
})
// Express routes
app.get("/", (req, res) => {
res.render("index")
})
httpServer.listen(port, () => {
console.log("Server is running on http://localhost:" + port)
})