-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
83 lines (66 loc) · 1.79 KB
/
App.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const express = require('express');
const util = require("util");
var http = require('http');
const {
join
} = require('path');
const {
emit
} = require('process');
const app = express();
const port = process.env.PORT || 3001;
var server = http.createServer(app);
const {
v4: uuidV4
} = require('uuid');
var io = require("socket.io")(server);
app.set('view engine', 'ejs');
app.use(express.static('public'));
//Middleware
app.use(express.json());
// app.use(cors())
app.get('/', (req, res) => {
res.redirect(`/${uuidV4()}`)
})
app.get("/:room", (req, res) => {
res.render('index', {
roomId: req.params.room
})
})
var clientsList = {};
io.on("connection", (socket) => {
console.log('Connected');
// socket.on('join-room',(roomId,userId)=>{
// console.log(roomId,userId);
// })
console.log(socket.id + 'has joined');
//MESSAGE EVENT
socket.on('message', (msg) => {
let targetId = msg.targetId;
console.log("TARGET ID " + targetId);
var obj_str = util.inspect(clientsList);
console.log(obj_str);
console.log("CLIENTSList " + clientsList);
console.log("THIS IS THE TARGET ID " + clientsList[targetId]);
console.log(msg);
if (clientsList[targetId]) {
clientsList[targetId].emit("message", {
"sourceId": msg.sourceId,
"message": msg.messageBody
});
}
});
//SIGN IN
socket.on('signIn', (id) => {
console.log("SIGN IN ID" + id);
clientsList[id] = socket;
console.log(clientsList[id]);
});
// socket.on("leave",(id)=>{
// socket.disconnect();
// clientsList = _.omit(clientsList,id);
// })
})
server.listen(port, '0.0.0.0', () => {
console.log('Server is running');
})