-
Notifications
You must be signed in to change notification settings - Fork 112
/
server.js
58 lines (46 loc) · 1.43 KB
/
server.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
const express = require('express')
const http = require('http')
const app = express()
const server = http.createServer(app)
const socket = require('socket.io')
const io = socket(server)
const username = require('username-generator')
const path = require('path')
const { AwakeHeroku } = require('awake-heroku');
AwakeHeroku.add({
url: "https://cuckooapp.herokuapp.com"
})
app.use(express.static('./client/build'));
app.get('*', (req,res)=>{
res.sendFile(path.resolve(__dirname, "client","build","index.html"));
})
const users={}
io.on('connection', socket => {
//generate username against a socket connection and store it
const userid=username.generateUsername('-')
if(!users[userid]){
users[userid] = socket.id
}
//send back username
socket.emit('yourID', userid)
io.sockets.emit('allUsers', users)
socket.on('disconnect', ()=>{
delete users[userid]
})
socket.on('callUser', (data)=>{
io.to(users[data.userToCall]).emit('hey', {signal: data.signalData, from: data.from})
})
socket.on('acceptCall', (data)=>{
io.to(users[data.to]).emit('callAccepted', data.signal)
})
socket.on('close', (data)=>{
io.to(users[data.to]).emit('close')
})
socket.on('rejected', (data)=>{
io.to(users[data.to]).emit('rejected')
})
})
const port = process.env.PORT || 8000
server.listen(port, ()=>{
console.log(`Server running on port ${port}`)
})