-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
64 lines (54 loc) · 1.83 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
const express = require('express')
const { createServer } = require('http')
const { Server } = require('socket.io')
const cors = require('cors')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
// // Database collections
// const Datastore = require('nedb')
// const users = new Datastore({ filename: './data/users', autoload: true })
// const rooms = new Datastore({ filename: './data/rooms', autoload: true })
// Constants
const port = process.env.PORT || 5300
const FRONTEND_URL = process.env.FRONTEND_URL || 'http://localhost:3000'
// Creating server
const app = express()
app.use(cors())
const httpServer = createServer(app)
const io = new Server(httpServer, {
cors: {
origin: true,
methods: ['GET', 'POST'],
},
})
// Data
const users = []
const rooms = []
// Importing route handlers - socket.io
const { login, logout } = require('./auth-handler')(io, { users, rooms })
const { createRoom, getRoom, joinRoom, addVideo, removeVideo, startRoom, setRoomInfo, stop, sync, leaveRoom } =
require('./room-handler')(io, { users, rooms })
// Assigning route handlers - socket.io
const onConnection = (socket) => {
// Auth events
socket.on('auth:login', login)
socket.on('auth:logout', logout)
// Room events
socket.on('room:create', createRoom)
socket.on('room:get', getRoom)
socket.on('room:join', joinRoom)
socket.on('room:addvideo', addVideo)
socket.on('room:removevideo', removeVideo)
socket.on('room:startroom', startRoom)
socket.on('room:setroominfo', setRoomInfo)
socket.on('room:stop', stop)
socket.on('room:sync', sync)
socket.on('room:leave', leaveRoom)
}
app.get('/ping', (req, res) => {
res.send('pong')
console.log('ping', req.ip)
})
// Starting http and socketio servers
io.on('connection', onConnection)
httpServer.listen(port, () => console.log(`Listening on http://localhost:${port}`))