client-service

0.0.4 • Public • Published

Client-service

Provide general headless browser service based on phantomjs.

Make sure you have installed the phantomjs.

Install

# intall client-service
npm install client-service

# install client-service-bridge
npm install client-service-bridge

Example

client.js(Phantomjs)

// You need to install "client-service-bridge".
var clientTools = require('client-service-bridge');
var webPage = require('webpage');
var page = webPage.create();

page.viewportSize = {
    width: 1920,
    height: 1080
};

page.open('http://phantomjs.org', function (status) {
  var base64 = page.renderBase64('PNG');
  clientTools.send(base64, function (status, res) {
  	phantom.exit();
  });
});

entry.js(Node)

var HeadlessBrowserEnv = require('client-service');

var inst = new HeadlessBrowserEnv();
var taskPath = 'xxxxx/client.js';
inst.run(taskPath, {}, {
    onMessage: function (bridgeMethod, bridgeData, resDataForBridge) {
        console.log(bridgeData);
    }
});

API

var hdlInst = new HeadlessBrowserEnv([bridgeApi], [opts])

  • bridgeApi: Used to communicate with bridge. Default http://127.0.0.1:3333;
  • opts:
    • opts.buildBridgeServer: Automatically create a bridge service. Default true;
    • opts.secret: secret for bridge api(compare with req.headers['x-secret']). * opts.timeout: overtime time for hdInst.run(xxx), default 10000 ms. * opts.concurrency: concurrent number for hdInst.run(xxx), default cpus length * 2.

hdlInst.run(jobFilePath, jobParams, [opts]);

  • jobFilePath: Js file which running on phantomjs environment.
  • jobParams: Parameters for jobFile.
  • opts.onMessage(bridgeMethod, bridgeData, resDataForBridge):
    • bridgeMethod: GET/POST;
    • bridgeData: clientTools.send(xxxx);
    • resDataForBridge: clientTools.send(..., function (xx, res) {});
  • opts.onTimeout: called when task is timeout.

hdlInst.verifyRequest(req);

Verify bridge request(If you set opts.buildBridgeServer = false, and create bridge server by yourself, you can use this method to verify request.).

hdlInst.resolveResponseData(req, reqParams);

Resolve response data for bridge(If you set opts.buildBridgeServer = false, and create bridge server by yourself, you can use this method to resolve data for response.).

CHANGE LOG

  • Add onTimeout;
  • Add concurrency for task's queue;

Package Sidebar

Install

npm i client-service

Weekly Downloads

5

Version

0.0.4

License

none

Last publish

Collaborators

  • bottleliu