-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
149 lines (122 loc) · 2.68 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
Chat application for @node.js
express version.
*/
//Load modules.
var express = require('express'),
server = require('http'),
io = require('socket.io'),
swig = require('swig'),
escape_html = require('escape-html'),
fs = require('fs');
//Load config.
console.log('Loading configuration.');
var config = fs.readFileSync('config.json');
var config = JSON.parse(config);
var port = config.port;
var views = config.views;
console.log('Configuration loaded.');
//Initiate express module in app.
var app = express();
//Global vars
var Title = "Node.js Chat";
app.engine('html', swig.renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
swig.setDefaults(
{
cache: false
});
app.get('/', function(request, response)
{
response.render('index',
{
'Title': Title
});
});
//logger.
app.use(function(request, response, next)
{
if(app.get('env') == 'development')
{
console.log('%s %s', request.method, request.url);
console.log('connection from: ' + request.ip);
}
next();
});
//Set directory for static files (css, js, img)
app.use(express.static(__dirname + '/public'));
//Run the app.
var server = server.createServer(app).listen(port);
server;
//Run the socket.
var io = io.listen(server);
io.set('log', 0);
var guest = 0;
var users = [];
//When socket connects.
io.sockets.on('connection', function(socket)
{
//Set Guest for the session.
socket.set('username', 'guest' + guest, function()
{
guest++;
var current = 'guest' + guest;
users.push('guest' + guest);
//Send the users connected.
io.sockets.emit('users',
{
users: li(users)
});
//When a message from the browser is recieved.
socket.on('Message_send', function(data)
{
//Escape all html tags.
var username = escape_html(data['username']),
message = escape_html(data['message']);
//Rename the guest for the username.
socket.set('username', username, function()
{
var index = users.indexOf(current);
current = username;
users[index] = current;
//Emit the message.
io.sockets.emit('Message_respond',
{
user: username,
message: message
});
//Send the users connected.
io.sockets.emit('users',
{
users: li(users)
});
});
});
});
//On disconnect/timeout.
socket.on('disconnect', function()
{
//Get the user disconnected.
socket.get('username', function(error, data)
{
var index = users.indexOf(data);
users.splice(index,1);
io.sockets.emit('users',
{
users: li(users)
});
});
});
});
//Transform all array to <li>tags.
function li(array)
{
var length = array.length,
result = '';
for (var i = 0; i < length; i++)
{
result += '<li>' + array[i] + '</li>';
}
return result;
}