-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
130 lines (108 loc) · 3.18 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require('express')
const http = require('http')
var cors = require('cors')
// cors give permission for resource
const app = express()
const bodyParser = require('body-parser')
const path = require("path")
var xss = require("xss")
// it prevent from attack cross site scripting
var server = http.createServer(app)
var io = require('socket.io')(server)
app.use(cors())
app.use(bodyParser.json())
// deprication
if(process.env.NODE_ENV==='production'){
app.use(express.static(__dirname+"/build"))
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname+"/build/index.html"))
})
}
app.set('port', (process.env.PORT || 4001))
sanitizeString = (str) => {
return xss(str)
}
// to modify string before goint production
connections = {}
messages = {}
timeOnline = {}
io.on('connection', (socket) => {
socket.on('join-call', (path) => {
if(connections[path] === undefined){
connections[path] = []
}
connections[path].push(socket.id)
timeOnline[socket.id] = new Date()
for(let a = 0; a < connections[path].length; ++a){
io.to(connections[path][a]).emit("user-joined", socket.id, connections[path])
// event fire broad cast event
}
if(messages[path] !== undefined){
for(let a = 0; a < messages[path].length; ++a){
io.to(socket.id).emit("chat-message", messages[path][a]['data'],
messages[path][a]['sender'], messages[path][a]['socket-id-sender'])
}
}
console.log(path, connections[path])
})
//
socket.on('signal', (toId, message) => {
io.to(toId).emit('signal', socket.id, message)
})
socket.on("upvote", (countMap) => {
console.log("inside app.js")
for(let a = 0; a < connections[key].length; ++a){
io.to(connections[key][a]).emit("upvote", countMap)
}
console.log("inside app.js")
});
socket.on('chat-message', (data, sender) => {
console.log("inside message app.js")
data = sanitizeString(data)
sender = sanitizeString(sender)
var key
var ok = false
for (const [k, v] of Object.entries(connections)) {
for(let a = 0; a < v.length; ++a){
if(v[a] === socket.id){
key = k
ok = true
}
}
}
// for multiple room
if(ok === true){
if(messages[key] === undefined){
messages[key] = []
}
messages[key].push({"sender": sender, "data": data, "socket-id-sender": socket.id})
console.log("message", key, ":", sender, data)
for(let a = 0; a < connections[key].length; ++a){
io.to(connections[key][a]).emit("chat-message", data, sender, socket.id)
}
}
})
socket.on('disconnect', () => {
var diffTime = Math.abs(timeOnline[socket.id] - new Date())
var key
for (const [k, v] of JSON.parse(JSON.stringify(Object.entries(connections)))) {
for(let a = 0; a < v.length; ++a){
if(v[a] === socket.id){
key = k
for(let a = 0; a < connections[key].length; ++a){
io.to(connections[key][a]).emit("user-left", socket.id)
}
var index = connections[key].indexOf(socket.id)
connections[key].splice(index, 1)
console.log(key, socket.id, Math.ceil(diffTime / 1000))
if(connections[key].length === 0){
delete connections[key]
}
}
}
}
})
})
server.listen(app.get('port'), () => {
console.log("listening on", app.get('port'))
})