-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteaparty.js
251 lines (191 loc) · 6.53 KB
/
teaparty.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
const WebSocket = require('ws');
const app = require('express')()
const http = require('http').createServer(app);;
const Discord = require('discord.js');
const client = new Discord.Client();
// const {config, token} = require('./config.json');
const { argv } = require('yargs');
// let channel
// client.once('ready', () => {
// console.log('Ready!');
// // channel = client.channels.find('name', 'teaparty')
// // channel.send('the teaparty has started')
// if(!argv.l){
// client.channels.cache.get(`698580923960131658`).send(`the teaparty has started`)
// }
// });
// client.login(token);
// 698580923960131658
// client.on('message', message => {
// // console.log(message.content);
// if (message.content === '!ping') {
// // send back "Pong." to the channel the message was sent in
// }
// });
// // Create an event listener for new guild members
// client.on('guildMemberAdd', member => {
// // Send the message to a designated channel on a server:
// const channel = member.guild.channels.cache.find(ch => ch.name === 'welcome');
// // Do nothing if the channel wasn't found on this server
// if (!channel) return;
// // Send the message, mentioning the member
// channel.send(`Welcome to the server, ${member}! We're a loose collective of artists who do internet-based performances.`);
// channel.send('Most of our events are open for participation. type ```\!list``` at any time to check the schedule, and click on a link to get involved with that particular event. Like So:');
// channel.send('!list')
// });
let listenPort = (process.env.PORT || 8090)
const wss = new WebSocket.Server({ 'server': http, clientTracking: true });
http.listen(listenPort, function(){
// console.log('listening on ' + listenPort);
})
//TODO: once we have multiple hosts running, need a way to acquire their addresses and run status
let guestlist = {
pals: {},
headcount: 0,
host: {
name: 'herokuHost',
ip: 'mischmasch-host.herokuapp.com'
}
}
let lookup = {}
// let id = 0;
// custom keepAlive function to detect and handle broken connections
function noop() {}
function heartbeat() {
this.isAlive = true;
}
const interval = setInterval(function ping() {
wss.clients.forEach(function each(ws) {
if (ws.isAlive === false) {
// console.log(ws)
return ws.terminate();
}
ws.isAlive = false;
ws.ping(noop);
});
}, 3000);
//
wss.on('connection', function connection(ws, req) {
let id = req.headers['sec-websocket-key'];
// console.log(id)
ws.isAlive = true;
ws.on('pong', heartbeat);
ws.on('message', function incoming(message) {
msg = JSON.parse(message)
switch (msg.cmd){
case 'newClient':
// prevent duplicate entries from client if it re-connects
Object.keys(lookup).forEach(function(key){
if(lookup[key] === msg.data.username){
delete lookup[key]
}
})
lookup[id] = msg.data.username
lookup[msg.data.username] = id
guestlist.pals[msg.data.username] = msg.data
// use this to keep count # of headcount on network
// tempCounter = 0
// Object.keys(lookup).forEach(function(key){
// tempCounter++
// })
guestlist.headcount = Object.keys(guestlist.pals).length
// guestlist.headcount = tempCounter / 2 // lookup always contains 2 entries per peer
// // console.log('number of clients', clients.headcount)
// if (guestlist.host === null){
// guestlist.host = msg.data.username
// } else {
// //?
// }
network = JSON.stringify({
cmd: 'guestlist',
data: guestlist,
date: Date.now()
})
broadcast(network)
console.log('\nclient update: ', guestlist, '\n\n')
// if(!argv.l){
// client.channels.cache.get(`698580923960131658`).send('updated guestlist: ' + Object.keys(guestlist.pals))
// }
break;
default:
// console.log(lookup[id] + ' sent unhandled message ' + msg)
ws.send('server received message but did not understand: ' + msg)
break;
}
});
ws.on('close', function(code, reason) {
clearInterval(interval);
// // console.log('ws id', id)
let d = lookup[id]
delete guestlist.pals[d]
// console.log('escorted ' + d + ' from teaparty')
// let msg = JSON.stringify({
// cmd: 'serverMsg',
// data: d,
// date: Date.now()
// })
// broadcast(msg)
// console.log('updated guestlist', guestlist)
let attendance = Object.keys(guestlist.pals).length
guestlist.headcount = attendance
// remove client info from list of active guestlist
// delete guestlist[id]
//guestlist.headcount = connections - 1
// if(!argv.l){
// client.channels.cache.get(`698580923960131658`).send('updated guestlist: ' + Object.keys(guestlist.pals))
// }
if(guestlist.headcount === 0){
// guestlist.host = null
console.log('\n\nthe party is over\n\n')
// ? msg the deltaWS host (mischmasch-host.herokuapp.com) and tell to store delta history, sceneFile, and then process.exit()?
return
}
// else if (guestlist.headcount === 1 || guestlist.host === d){
// // if only one client remains or client that just disconnected was host, automatically assign a new host
// for (var prop in guestlist.pals) {
// // object[prop]
// // console.log(prop + ' is now the teaparty host')
// // for now, randomly select next host:
// guestlist.host = prop
// break;
// }
// } else {
// }
let clientUpdate = JSON.stringify({
cmd: 'guestlist',
data: guestlist,
date: Date.now()
})
broadcast(clientUpdate)
console.log('guestlist update', guestlist)
})
});
// ping the pals/host -- solves a bug where heroku apps will crash if no response after nn seconds
// so ping all th pals/host!
setInterval(() => {
wss.clients.forEach((client) => {
client.send(JSON.stringify({
cmd: 'ping',
data: guestlist,
date: Date.now()
}))
});
}, 3000);
function broadcast(msg){
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(msg);
}
});
}
function updateNetwork(msg){
network = JSON.stringify({
cmd: 'network',
data: msg,
date: Date.now()
})
broadcast(network)
}
// use this to reset the signal server from a client.
function hardReset(){
}