-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (73 loc) · 2.19 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
var express = require('express');
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const randomstring = require("randomstring");
const randomColor = require('randomcolor'); // import the script
app.use(express.static('public'))
app.get('/', function(req, res) {
res.sendfile('index.html');
});
app.get('/portal', function(req, res) {
res.sendfile('portal.html');
});
app.get('/helloworld', function(req, res) {
res.sendfile('helloworld.html');
});
app.get('/list', function(req, res) {
res.sendfile('list.html');
});
'use strict';
// const fs = require('fs');
// let rawdata = fs.readFileSync('data.json');
// let views = JSON.parse(rawdata).views;
// console.log(views);
let views = 0
data = {}
newClient = () => {
n = new Object();
let id = randomstring.generate()
data[id.toString()] = new Object()
data[id]["attributes"] = new Object()
// data[id]["attributes"]["color"] = randomColor().toString()
return id
}
function update() {
let obj = new Object()
obj.views = views
let update = JSON.stringify(obj);
fs.writeFileSync('data.json', update);
}
// const myVar = setInterval(update, 30000);
let clients = Object.keys(data).length;
io.on('connection', function(socket) {
views++
let id = newClient()
socket.emit('welcome',{ msg: id})
clients++;
io.sockets.emit('broadcast',{ msg: clients + ' clients connected!', views: views + ' lifetime sessions'});
socket.on('update', function(d) {
if (data[d.msg.id]) {
data[d.msg.id.toString()]["attributes"] = d.msg.attributes
io.sockets.emit('change',data)
}
})
message = () => {
msg = randomstring.generate()
io.sockets.emit('message',{msg: msg})
console.log(msg)
}
// const testChat = setInterval(message, 3000)
socket.on('message', function(data) {
socket.broadcast.emit('message',{msg: data.msg})
})
socket.on('disconnect', function () {
clients--;
io.sockets.emit('delete', {id: id})
delete data[id]
io.sockets.emit('broadcast',{ msg: clients + ' clients connected!'});
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});