-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
47 lines (37 loc) · 1.28 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
//require necessary modules
var http = require('http');
var express = require('express');
var socketIO = require("socket.io");
//initialize our application
var app = express();
var server = http.createServer(app).listen(8080);
var io = socketIO.listen(server);
//settings
var settings = {
'view_directory': '/views',
'stylesheets_directory': '/assets/css',
'javascript_directory': '/assets/js'
}
app.get('/', function(request, response){
response.sendfile(__dirname + settings.view_directory + '/index.html')
});
app.get('/*.css', function(request, response){
response.sendfile(__dirname + settings.stylesheets_directory + request.originalUrl)
});
app.get('/*.js', function(request, response){
response.sendfile(__dirname + settings.javascript_directory + request.originalUrl)
});
//chat using socket.io
io.sockets.on('connection', function(client){
//when client sends a join event
client.on('join', function(data){
client.set('nickname', data);
client.broadcast.emit('message', { message: data + " just joined!", nickname: "Server Announcement" });
});
//when client sends a message
client.on('message', function(data){
client.get('nickname', function(err, nickname){
client.broadcast.emit('message', { message: data, nickname: nickname });
});
});
})