1

做这个教程,我不想device.js只在里面放一行:module.exports = "<a1 ..

在此处输入图像描述

如何将此设备 ID 放在我的主脚本中,并将其传递给apnagent对象?试过这个,但我想这就是连接没有建立的原因。

也许而不是module.exports = "<388 ..我需要类似的东西agent.set('something', and here the device ID);

var pfx = '/var/lib/openshift/555dd1415973ca1660000085/app-root/runtime/repo/pfx.p12';
module.exports = "<38873D3B 0D61A965 C1323D6C 0A9F2866 D1BB50A3 64F199E5 483862A6 7F02049C>"; // <----- THIS HERE

var apnagent = require('apnagent')
var agent = module.exports = new apnagent.Agent();
agent.set('pfx file', pfx);
// our credentials were for development
agent.enable('sandbox');

console.log('LOG1');
console.log(agent);
agent.connect(function (err) {

console.log('LOG2');
  // gracefully handle auth problems
  if (err && err.name === 'GatewayAuthorizationError') {
    console.log('Authentication Error: %s', err.message);
    process.exit(1);
  }

  // handle any other err (not likely)
  else if (err) {
    throw err;
  }

  // it worked!
  var env = agent.enabled('sandbox')
    ? 'sandbox'
    : 'production';

  console.log('apnagent [%s] gateway connected', env);
});
4

1 回答 1

1

你可以试试这个:

var pfx = '/var/lib/openshift/555dd1415973ca1660000085/app-root/runtime/repo/pfx.p12';
var deviceId = "<38873D3B 0D61A965 C1323D6C 0A9F2866 D1BB50A3 64F199E5 483862A6 7F02049C>"; // <----- THIS HERE

var apnagent = require('apnagent')
var agent = new apnagent.Agent(deviceId);
agent.set('pfx file', pfx);
// our credentials were for development
agent.enable('sandbox');

console.log('LOG1');
console.log(agent);
agent.connect(function (err) {

console.log('LOG2');
  // gracefully handle auth problems
  if (err && err.name === 'GatewayAuthorizationError') {
    console.log('Authentication Error: %s', err.message);
    process.exit(1);
  }

  // handle any other err (not likely)
  else if (err) {
    throw err;
  }

  // it worked!
  var env = agent.enabled('sandbox')
    ? 'sandbox'
    : 'production';

  console.log('apnagent [%s] gateway connected', env);
});

解释是这样的:module.exports 是通用的模块导出语法。通过这种方式,您可以避免导出模块并通过在代码中手动硬编码 deviceId 来导入它。

于 2016-11-16T11:06:46.520 回答