-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
102 lines (92 loc) · 3.07 KB
/
server.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
/** REQUIREMENTS **/
var CryptoJS = require("crypto-js");
var AES = require("crypto-js/aes");
var SHA256 = require("crypto-js/sha256");
var casual = require("casual");
var express = require("express");
var app = express();
//var bot = require('./bot');
/** NODEJS PORT LISTENING ** /
var port = 3700;
/** HEROKU PORT LISTENING **/
app.listen(3700);
var port = parseInt(process.argv[2]);
/**/
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) {
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});
user.emit('assign', {user: randomUsername, message: 'assign'});
console.log('LINE - ' + JSON.stringify(allUsers));
io.sockets.emit('LINE - ', JSON.stringify(allUsers));
/*
This is redundant. I think? Seems to be handled by
io.sockets.on('connection',
Keep around for a few versions to just makes sure.
*/
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 randomMessage = function () {
function randomBotString() {
var chars = "!@#$%^&*|\/?~=-0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = (Math.random() * 1028);
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
/*var botusername = "username";*/
var sentence = "U2FsdGVkX" /** "Salted," I really need to fix this. **/+ randomBotString();
var details = {/*user: botusername, */message: sentence };
var botData = details;
return botData;
/** NEW CODE END **/
}
var randomChat = function () {
var randomInterval = Math.floor(Math.random() * 25000) + 1;
io.sockets.emit('message', 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('/');
$("#botWarning").show();
});
app.get("/removeBots", function(req, res){
clearTimeout(botsJob);
res.redirect('/');
$("#botWarning").hide();
});
console.log("Listening on port " + port);