在噩梦中重用电子会话 [英] Reuse electron session with nightmare

查看:72
本文介绍了在噩梦中重用电子会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Nightmare.js 打印 pdf.我向节点服务器发送请求并构建页面,使用 Nightmare 确保页面已加载,然后打印 pdf.但是对于每个请求我创建一个新的电子窗口,我如何重用相同的窗口或最大 X 个电子窗口的池来处理我的 pdf 打印?

I am using Nightmare.js to print pdf's. I send a request to the node server and build the page, using Nightmare to ensure the page has loaded, then print a pdf. But for each request I create a new electron window, how do I reuse the same window, or a pool of max X electron windows, to handle my pdf printing?

var nightmare = require('nightmare'),
    http = require('http');

function createPage(o, final) {

    var page = nightmare()
    .goto('file:\\\\' + __dirname + '\\index.html');
    .wait(function () {
        return !!(window.App && App.app); //Check Javascript has loaded
    })

    page.evaluate(function (template, form, lists, printOptions) {
        App.pdf.Builder.create({
            //args for building pages
        });
    }, o.template, o.form, o.lists, o.printOptions);

    page.wait(function () {
        return App.pdf.Builder.ready;
    })
    .pdf(form.filename, { "pageSize": "A4", "marginsType": 1 })
    .end()
    .then(function () {
        console.log('Pdf printed');
        final(true);
    })
    .catch(function (err) {
        console.log('Print Error: ' + err.message);
    });
}

http.createServer(function (request, response) {
    var body = [];
    request.on('data', function (chunk) {
        body.push(chunk);
    }).on('end', function () {
        body = Buffer.concat(body).toString();
        var json = JSON.parse(body);
        createPage(json, function (status) {
            if (status === true) {
                response.writeHead(200, { 'Content-Length': 0 });
            } else {
                response.writeHead(500, { 'Content-Type': 'text/html' });
                response.write(' ' + status);
                console.log('status error: ' + status);
            }
            response.end('End of Request \n'); //return status msg, if any
        });
    });
}).listen(8007);

我知道在上一次打印完成之前再次使用同一个电子窗口可能会出现的所有并发问题,所以我希望得到答案以明确说明如何避免这种情况.

I am aware of all the concurrency issues that might arise by potentially using the same electron window again before a previous print has finished, so I would like the answer to make it clear how that is avoided.

推荐答案

你需要创建一次噩梦实例,而不是在循环中.

You need to create the nightmare instance once and not in a loop.

var page = nightmare()
    .goto('file:\\\\' + __dirname + '\\index.html');

每次创建页面时都会创建新的噩梦实例.您可以创建一次实例

This creates new nightmare instance each time you create a page. You can create instance once

const Nightmare = require('nightmare');
const browser = Nightmare();

然后每次使用 browser.goto(url).您可以使用以下给出的答案链接您的 goto 语句:https://github.com/segmentio/nightmare/issues/708

Then use it each time with browser.goto(url). You can chain your goto statment using answers given at: https://github.com/segmentio/nightmare/issues/708

摘自其中一个答案:

function run() {
  var nightmare = Nightmare();
  yield nightmare
    .goto('https://www.example.com/signin')
    .type('#login', 'username')
    .type('#password', 'password')
    .click('#btn')

  for (var i = 0; i < 4; i++) {
    yield nightmare
      .goto('https://www.example.com/page'+i)
      .wait(1000)
      .evaluate(function(){
        return $('#result > h3').text()
      })
  }

  yield nightmare.end()
}

您还可以创建多个浏览器,以进行池化.

You can also create multiple browsers, for pooling as well.

var browser1 = Nightmare();
var browser2 = Nightmare();

这篇关于在噩梦中重用电子会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆