This repository has been archived by the owner on Mar 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
86 lines (65 loc) · 2.09 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
// Include the required modules
var express = require('express'),
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
// Let the app listen op port 1337
server.listen(1337);
// Set the console message
console.log('Application accessible at http://localhost:1337');
// Configure the application
app.configure(function() {
// Enable the bodyParser so we can access POST data
app.use(express.bodyParser());
// Enable the cookieParser so we can work with cookies
app.use(express.cookieParser('super-duper-secret'));
// add req.session cookie support
app.use(express.cookieSession());
// Enable the logging
app.use(express.logger('dev'));
// Set the path to the public directory
app.use(express.static(__dirname + '/public'));
// Set the views directory
app.set('views', __dirname + '/views');
// Set the view engine
app.set('view engine', 'jade');
});
// Todo index page (homepage)
app.get('/', function(req, res) {
res.render('index');
});
var getTimestamp = function() {
// Set the currentdate
var curDate = new Date();
var hours = curDate.getHours();
var minutes = (curDate.getMinutes() < 10 ? '0' : '') + curDate.getMinutes();
return hours + ':' + minutes;
}
// Contains all clients
var clients = {};
io.sockets.on('connection', function(socket) {
// Add the client to the list of clients
clients[socket.id] = socket;
// Send a welcome message to the new user
socket.emit('newMessage', {
'message': '<span stlye="color: #808080">Welcome to the chat!</span>',
'timestamp': getTimestamp()
});
// Notify everyone that a new user has connected
socket.broadcast.emit('newMessage', {
'message': '<span stlye="color: #808080">A new user connected..</span>',
'timestamp': getTimestamp()
});
// A user has clicked on the link
socket.on('msg', function(data) {
// Strip the HTML tags
var message = data.message.replace(/<(?:.|\n)*?>/gm, '');
if (message.length > 1 && message.length < 500) {
// Send back a message
io.sockets.emit('newMessage', {
'message': message,
'timestamp': getTimestamp()
});
}
});
});