-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (77 loc) · 2.66 KB
/
Copy pathapp.js
File metadata and controls
86 lines (77 loc) · 2.66 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
77
78
79
80
81
82
83
84
85
86
var http = require('http');
var io = require('socket.io');
var fs = require('fs');
var url = require('url');
var server = http.createServer(function (req, res) {
var pathname = url.parse(req.url).pathname;
console.log('Request for ' + pathname + ' received.');
route(pathname, req, res);
});
function route (pathname, req, res) {
if (pathname === '/static/jquery.mobile-1.3.2.min.css') {
fs.readFile('statics/jquery.mobile-1.3.2.min.css',
{ encoding: 'utf-8' }, function (err, data) {
if (err) {
res.end('Error');
} else {
res.writeHead(200, {'content-type': 'text/css'});
res.end(data, 'utf-8');
}
});
} else if (pathname === '/static/jquery.mobile-1.3.2.min.js') {
fs.readFile('statics/jquery.mobile-1.3.2.min.js',
{ encoding: 'utf-8' }, function (err, data) {
if (err) {
res.end('Error');
} else {
res.writeHead(200, {'content-type': 'application/x-javascript'});
res.end(data, 'utf-8');
}
});
} else if (pathname === '/static/jquery-1.8.3.min.js') {
fs.readFile('statics/jquery-1.8.3.min.js',
{ encoding: 'utf-8' }, function (err, data) {
if (err) {
res.end('Error');
} else {
res.writeHead(200, {'content-type': 'application/x-javascript'});
res.end(data, 'utf-8');
}
});
} else if (pathname === '/favicon.icon') {
res.end('No icon');
} else {
fs.readFile('views/index.html', { encoding: 'utf-8' }, function (err, data) {
if (err) {
res.end('Error');
} else {
res.writeHead(200, {'content-type': 'text/html'});
res.end(data, 'utf-8');
}
});
}
}
var server_socket = io.listen(server);
server.listen(3000);
var client_list = [];
server_socket.on('connection', function (client_socket) {
client_socket.nickname = new Date().getTime();
client_list.push(client_socket);
client_socket.on('my_msg', function (msg) {
broadcast(msg, client_socket);
});
client_socket.on('disconnect', function () {
var index = client_list.indexOf(client_socket);
client_list.splice(index, 1);
});
});
function broadcast(msg, client_socket) {
for (var i = 0; i < client_list.length; ++i) {
if (client_list[i] !== client_socket) {
client_list[i].emit('others_msg', {
nickname: client_socket.nickname,
msg: msg
});
}
}
}