forked from JCMais/node-libcurl
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path04-multi.js
94 lines (76 loc) · 2.3 KB
/
04-multi.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
/**
* Copyright (c) Jonathan Cardoso Machado. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Example showing how to use the `Multi` handle to make async requests.
* The Multi handle is used internally by the `Curl` wrapper
*
* The methods should have a description, hover over them to see it on your editor
*/
const { Easy, Multi } = require('../dist')
const urls = [
'http://google.com',
'http://bing.com',
'http://msn.com',
'http://ask.com/',
]
const multi = new Multi()
const handles = []
const handlesData = []
let finished = 0
multi.onMessage((error, handle, errorCode) => {
const responseCode = handle.getInfo('RESPONSE_CODE').data
const handleData = handlesData[handles.indexOf(handle)]
const handleUrl = urls[handles.indexOf(handle)]
let responseData = null
console.log('# of handles active: ' + multi.getCount())
if (error) {
console.log(
handleUrl +
' returned error: "' +
error.message +
'" with errcode: ' +
errorCode,
)
} else {
for (let i = 0; i < handleData.length; i++) {
responseData += handleData[i].toString()
}
console.log(handleUrl + ' returned response code: ' + responseCode)
console.log(
handleUrl + ' returned response body length: ' + responseData.length,
)
}
// we are done with this handle, remove it from the Multi instance and close it
multi.removeHandle(handle)
handle.close()
if (++finished === urls.length) {
console.log('Finished all requests!')
// remember to close the multi instance too, when you are done with it.
multi.close()
}
})
/**
* This will be used as callback for for WRITEFUNCTION
*
* data is a Buffer, n and nmemb are integers. You must return n * nmemb from this
* callback to let libcurl know you handled correctly all data received.
*/
function onData(data, n, nmemb) {
// this === the handle
const key = handles.indexOf(this)
handlesData[key].push(data)
return n * nmemb
}
for (let i = 0; i < urls.length; i++) {
const handle = new Easy()
handle.setOpt('URL', urls[i])
handle.setOpt('FOLLOWLOCATION', true)
handle.setOpt('WRITEFUNCTION', onData)
handlesData.push([])
handles.push(handle)
multi.addHandle(handle)
}