admin管理员组文章数量:1398147
This code send the first emit to client and client get the messageStart : 'Job is starting...' This is OK. After that the code launch puppeteer and make the screen shot example.png. This is OK too. But the second emit is not fired and not send to client. In the console.log of the server I get :
- job is starting
- CAPTURE FINISHED
- job is finished
This is OK too.
What happened? Why is the second emit not fired?
const express = require('express');
const puppeteer = require('puppeteer')
const app = express();
const server = app.listen(3000);
app.set('view engine', 'ejs');
var io = require('socket.io').listen(server);
app.get('/', function (req, res, next) {
res.render('index');
});
app.get('/scan', function (req, res, next) {
console.log('job is starting');
io.sockets.on('connection', function (socket) {
socket.emit('messageStart', 'Job is starting...');
});
(async () => {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto('');
await page.screenshot({ path: 'example.png' });
await browser.close();
console.log('CAPTURE FINISHED');
})().then(()=>{
console.log('job is finished');
io.sockets.on('connection', function (socket) {
socket.emit('messageEnd', 'Job is done!');
});
});
res.render('scan');
});
This code send the first emit to client and client get the messageStart : 'Job is starting...' This is OK. After that the code launch puppeteer and make the screen shot example.png. This is OK too. But the second emit is not fired and not send to client. In the console.log of the server I get :
- job is starting
- CAPTURE FINISHED
- job is finished
This is OK too.
What happened? Why is the second emit not fired?
const express = require('express');
const puppeteer = require('puppeteer')
const app = express();
const server = app.listen(3000);
app.set('view engine', 'ejs');
var io = require('socket.io').listen(server);
app.get('/', function (req, res, next) {
res.render('index');
});
app.get('/scan', function (req, res, next) {
console.log('job is starting');
io.sockets.on('connection', function (socket) {
socket.emit('messageStart', 'Job is starting...');
});
(async () => {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto('https://example.');
await page.screenshot({ path: 'example.png' });
await browser.close();
console.log('CAPTURE FINISHED');
})().then(()=>{
console.log('job is finished');
io.sockets.on('connection', function (socket) {
socket.emit('messageEnd', 'Job is done!');
});
});
res.render('scan');
});
Share
Improve this question
edited May 19, 2020 at 16:19
Pedro
asked May 18, 2020 at 16:26
PedroPedro
531 gold badge3 silver badges7 bronze badges
2 Answers
Reset to default 2You need to listen to the connection once and emit to the socket twice.
const scanner = async () => {
const browser = await puppeteer.launch({headless:false});
const page = await browser.newPage();
await page.goto('https://example.');
await page.screenshot({ path: 'example.png' });
await browser.close();
console.log('CAPTURE FINISHED');
}
app.get('/scan', async function (req, res, next) {
// emit to all clients on start
console.log('job is starting');
io.emit('messageStart', 'Job is starting...');
// do the actual stuff
await scanner();
// emit to all clients on finish
console.log('job is finished');
io.emit('messageEnd', 'Job is done!');
res.render('scan');
});
Here the plete code based on code of Md. Abu Taher :
const scanner = async () => {
// emit this message when the scan really starts
io.sockets.on('connection', function (socket) {
io.emit('messageDoing', 'Doing the Job ...');
});
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.goto('https://example.');
await page.screenshot({ path: 'example.png' });
await browser.close();
console.log('CAPTURE FINISHED');
};
app.get('/scan', async function (req, res, next) {
// emit to all clients on start
console.log('job is starting');
io.sockets.on('connection', function (socket) {
io.emit('messageStart', 'Job is starting...');
});
// do the actual stuff
await scanner();
// emit to all clients on finish
console.log('job is finished');
io.sockets.on('connection', function (socket) {
io.emit('messageEnd', 'Job is done...');
});
res.render('scan')
});
本文标签: javascriptasyncawait with socketioStack Overflow
版权声明:本文标题:javascript - asyncawait with socket.io - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744172494a2593848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论