forked from satrong/node-crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
76 lines (68 loc) · 2.08 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
var cp = require('child_process');
var config = require('./config.js');
if (config.mode === 'console') {
cp.fork('./crawler.js');
return;
}
var http = require('http');
var net = require('net');
var fs = require('fs');
var path = require('path');
var url = require('url');
var port = 8000;
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ port: 8001 });
var MIME_TYPES = {
'.txt': 'text/plain',
'': 'text/plain',
'.html': 'text/html',
'.css': 'text/css',
'.js': 'application/javascript',
'.json': 'application/json',
'.jpg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif'
};
var worker;
http.createServer(function (req, res) {
var filepath = './res' + (req.url === '/'?'/index.html':req.url);
console.log(filepath);
fs.readFile(filepath, function (err, body) {
if (err) {
res.writeHead(404);
} else {
res.writeHead(200, {
'Content-Type': MIME_TYPES[path.extname(filepath)],
'Content-Length': body.length,
'X-Powered-By': 'chc'
});
res.write(body);
}
res.end();
});
}).listen(port, function () {
console.log('监听端口%s中', port);
});
var worker;
wss.on('connection', function connection(_ws) {
_ws.on('message', function incoming(message) {
console.log('received: %s', message);
if (message === 'start') {
worker = cp.fork('./crawler.js');
worker.on("message", function (data) {
console.log(data)
_ws.send(data);
});
worker.on("close", function (code, signal) {
!code && _ws.send(JSON.stringify({ color: 'redBG', info: '已手动停止抓取' }));
});
} else if (message === "stop") {
worker.kill();
}
//ws.send('you said:' + message);
/// 对所有客户端发送信息
/*wss.clients.forEach(function each(client) {
client.send('you said:' + message);
});*/
});
});