-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (134 loc) · 4.03 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* eslint-disable no-console */
const http = require('http')
const express = require('express')
const path = require('path')
const uuid = require('uuid')
const app = express()
const server = http.createServer(app)
const socketio = require('socket.io')
const Session = require('./session')
const io = socketio()
app.io = io
io.attach(server)
// note: this is real simple. use a db instead of this ;)
let sessions =[]
const newsession = (id) =>{
sessions.push(new Session(id))
}
const publicSessions = () =>{
return sessions.filter(i => i.display === true)
}
app.use('/client', express.static(path.join(__dirname, '/client')))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client/index.html')
})
app.get('/s/:id', (req,res)=>{
let s = sessions.filter(i=>i.id === req.params.id)
/*if(s.length !== 1){
// no session exists with id, redirect to landing page
res.redirect('/')
} else{
res.sendFile(__dirname + '/client/client.html')
}*/
res.sendFile(__dirname + '/client/client.html')
})
app.get('/admin/:id', (req,res)=>{
let s = sessions.filter(i=>i.id === req.params.id)
if(s.length !== 1){
// no session exists with id, redirect to landing page
res.redirect('/')
} else{
res.sendFile(__dirname + '/client/admin.html')
}
})
io.on('connection', (socket) => {
console.log('client connected')
if(socket.handshake.query.room !== undefined){
console.log(`client wants to join the session room: ${socket.handshake.query.room}`)
socket.join(socket.handshake.query.room)
}
socket.on('disconnect', ()=>{
console.log('client disconnected')
})
// messages from index and client
socket.on('client',(ev)=>{
switch(ev.cmd){
case 'clienttime':{
// send the current client time to admins
socket.to(ev.id).emit('scmd', {cmd: 'clienttime', time: ev.time})
// update time in sessions once in a while
let sidx = sessions.findIndex(i=>i.id === ev.id)
if(sidx > -1){
sessions[sidx].time = ev.time
}
}
break
case 'clientstate':{
// send the current client time to admins
socket.to(ev.id).emit('scmd', {cmd: 'clientstate', ticking: ev.ticking})
// update time in sessions once in a while
let sidx = sessions.findIndex(i=>i.id === ev.id)
if(sidx > -1){
sessions[sidx].ticking = ev.ticking
}
}
break
case 'getsessions':
//reply to the sender
socket.emit('updatesessionlist', {sessions: publicSessions()})
break
case 'newsession':{
let sid = ''
if(ev.id !== undefined && ev.id.length > 0){
sid = ev.id
} else {
sid = uuid()
}
newsession(sid)
//console.log(sessions)
socket.emit('sessioncreated', {id: sid})
// this time emit for everyone (let other clients to update their session list)
//io.emit('updatesessionlist', {sessions: sessions})
}
break
case 'getsession':{
let s = sessions.filter(i=>i.id === ev.id)
if(s.length>0 && s.length === 1){
socket.emit('scmd', {id: ev.id, cmd: 'sessiondata', err: null, data: s})
} else{
socket.emit('scmd',{id: ev.id, cmd: 'sessiondata', err: 'no session'})
}
}
break
default:
console.log(`ERROR: invalid user command: ${ev.cmd}`)
}
})
// messages from adminclient
socket.on('adminclient',(ev)=>{
console.log(`admin command for session: ${ev.id}, emitting it to room`)
socket.to(ev.id).emit('scmd', ev)
let sidx = sessions.findIndex(i=>i.id === ev.id)
if(sidx > -1){
switch(ev.cmd){
case 'publishsession':
sessions[sidx].public = true
io.emit(io.emit('updatesessionlist', {sessions: publicSessions()}))
break
case 'start':
sessions[sidx].ticking = true
break
case 'pause':
sessions[sidx].ticking = false
break
case 'settime':
sessions[sidx].time = ev.time
break
}
}
//console.log(sessions[sidx])
})
})
server.listen(3000, function(){
console.log('listening on *:3000')
})