Files
  • index.js
  • client
  • db.js
  • db.sqlite3
  • oldfile
  • package-lock.json
  • package.json
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
const express = require('express');
const bodyParser = require('body-parser');
const db = require('./db');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static('public'));
app.use('/client',express.static(__dirname + '/client'));
app.get('/', (req, res) => {
	res.sendFile(__dirname+"/client/index.html");
});
var User = function (id){
  self = {
    id:id,
    name:id
  }
  return self;
}
var serv=require('http').Server(app);
var ALL_SOCKETS={};
var ALL_USERS={};
serv.listen(process.env.PORT);
var io=require('socket.io')(serv);
io.sockets.on('connection',(socket)=>{
  currentSocket=Math.random();
  console.log("Welcome user "+currentSocket+" to the server!");
  var user=new User(currentSocket);
  ALL_USERS[currentSocket]=user;
  ALL_SOCKETS[currentSocket]=socket;
  socket.on('disconnect',()=>{
    console.log("User "+user.id+" disconnected. Removing from list.")
    delete ALL_SOCKETS[user.id];
    delete ALL_USERS[user.id];
  });
  socket.on('username',(username)=>{
    if(username!=""){user.name=username;}
    else{user.name=user.id;}
  })
});
setInterval(()=>{
  allUsers=[]
  for(n in ALL_USERS){
    allUsers.push(ALL_USERS[n].name);
  }
  for(q in ALL_SOCKETS){
    ALL_SOCKETS[q].emit("allusers",allUsers);
  }
},1000)