1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00
examples/server-sent-events/drain.js

43 lines
922 B
JavaScript
Raw Normal View History

const http = require('http')
2022-08-07 02:24:40 +02:00
let drop_goal = 5_000
let dropped = 0
let query = {
2022-08-07 02:24:40 +02:00
method: 'POST',
host: '127.0.0.1',
port: 8080,
path: '/events',
}
setInterval(() => {
2022-08-07 02:24:40 +02:00
if (dropped < drop_goal) {
let request = http
.request(query, (response) => {
response.on('data', (data) => {
if (data.includes('data: connected\n')) {
// drop connection after welcome message
dropped += 1
request.abort()
}
})
2022-08-07 02:24:40 +02:00
})
.on('error', () => {})
.end()
}
}, 1)
setInterval(() => {
2022-08-07 02:24:40 +02:00
http
.post('http://127.0.0.1:8080/', () => print_status(true))
.setTimeout(100, () => print_status(false))
.on('error', () => {})
}, 20)
function print_status(accepting_connections) {
2022-08-07 02:24:40 +02:00
process.stdout.write('\r\x1b[K')
process.stdout.write(
`Connections dropped: ${dropped}, accepting connections: ${accepting_connections}`
)
}