forked from spencerthayer/TorchNoteJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
76 lines (66 loc) · 2.28 KB
/
server.js
File metadata and controls
76 lines (66 loc) · 2.28 KB
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
var cryptojs = require("cryptojs");
var express = require("express");
var bot = require('./bot');
var app = express();
/** PROPER PORT LISTENING ** /
var port = 3700;
/** HACK! TURN ON FOR HEROKU USE **/
var port = parseInt(process.argv[2]);
app.listen(3700);
/**/
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/public'));
var io = require('socket.io').listen(app.listen(port));
//var randomUsername = '';
var allUsers = [];
io.sockets.on('connection', function (user) {
//var x = Math.floor(Math.random() * 999) + 1;
//var randomUsername = '#' + x;
//var randomColor = "#000000".replace(/0/g,function(){return (~~(Math.random()*16)).toString(16);});
function randomColor() {
var color = (function lol(m, s, c) {
return s[m.floor(m.random() * s.length)] +
(c && lol(m, s, c - 1));
})(Math, /*'12345*/'6789ABCDEF', 4);
return color;
}
var randomUsername = '#' + randomColor();
allUsers.push({id: user.id, name: randomUsername});
console.log('LINE - ' + JSON.stringify(allUsers));
user.emit('assign', {user: randomUsername, message: 'assignn'});
// not needed: handled by io.sockets.on('connection', ?
//user.on('notifyConnected', function (data) {
//console.log('CONNECTED - ' + JSON.stringify(allUsers));
//});
user.on('disconnect', function (data) {
var userId = user.id;
allUsers.splice(allUsers.indexOf(userId), 1);
console.log('DISCONNECTED -' + JSON.stringify(allUsers));
});
// new message: broadcast it to all other users (excluding self)
user.on('send', function (data) {
user.broadcast.emit('message', data);
});
});
// generate random bots sending random messages on a random interval
var botsJob;
var randomChat = function () {
var randomInterval = Math.floor(Math.random() * 500) + 1;
io.sockets.emit('message', bot.randomMessage());
botsJob = setTimeout(randomChat, randomInterval);
}
randomChat();
// routes
app.get("/", function(req, res){
res.render("index.html");
});
app.get("/addBots", function(req, res){
randomChat();
res.redirect('/');
});
app.get("/removeBots", function(req, res){
clearTimeout(botsJob);
res.redirect('/');
});
console.log("Listening on port " + port);