-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·51 lines (41 loc) · 1.26 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
const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const moment = require('moment');
let userName;
let roomId = [];
let users = []
app.use(express.urlencoded({extended: false}))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/pages/login.html');
});
app.post('/home', (req, res) => {
res.sendFile(__dirname + '/pages/home.html' );
userName = req.body.name;
});
app.get('/admin', (req, res) => {
res.sendFile(__dirname + '/pages/admin.html' );
});
io.on('connection', (socket) => {
console.log('a user connected');
socket.on("user_info", async (data) => {
socket.join("rid_"+userName); // creating a room id
users.push({"name": userName , "id": socket.id, "room": "rid_"+userName})
});
socket.on('client_message', (msg) => {
// to send back to client
socket.emit("server_message", msg);
// to send to admin
socket.broadcast.emit("server_message", msg);
});
// when disconnected
socket.on('disconnect', (data) => {
console.log('user disconnected');
});
});
server.listen(3000, () => {
console.log('listening on 3000');
});