1

当我通过命令行运行我的服务器时,它的node server.js运行方式与我使用 upstart 脚本启动它时的运行方式不同(特别是推送通知的工作方式)。我应该如何修改我的新贵脚本来解决这个问题?

这是一个示例服务器,其中推送在新贵中失败与在命令行中成功

var apn = require('apn');
var options = {
  passphrase:'certpassphrase'
};
var err = null;
var apnConnection = new apn.Connection(options);

apnConnection.on('error', function(error){
  console.log("apscallback", 1, error);
});

apnConnection.on('transmitted', function(notification){
  console.log("apscallback", 2);
  console.log(notification);
});

apnConnection.on('timeout', function(){
  console.log("apscallback", 3);
});

apnConnection.on('connected', function(){
  console.log("apscallback", 4);
});

apnConnection.on('disconnected', function(){
  console.log("apscallback", 5);
});

apnConnection.on('socketError', function(error){
  console.log("apscallback", 6, error);
});

apnConnection.on('transmissionError', function(errorCode, notification){
  console.log("apscallback", 7, errorCode);
});

apnConnection.on('cacheTooSmall', function(difference){
  console.log("apscallback", 8);
});

function sendTestNotification() {
  var deviceToken = 'mydevicetoken';
  var myDevice = new apn.Device(deviceToken);
    var note = new apn.Notification();

    note.expiry = Math.floor(Date.now() / 1000) + 60; // Expires 1 hour from now.
    // note.badge = 3;
    // note.sound = "ping.aiff";
    // note.alert = "\uD83D\uDCE7 \u2709 You have a new message";
    note.payload = {'message': 'somemessage', 'requester': 7};
    apnConnection.pushNotification(note, myDevice);
}

sendTestNotification();


http.listen(port, function(){
  console.log('listening on *:', port);
});

这是我的新贵脚本的样子:

#!upstart
description "My Server"

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

env NODE_ENV=production
chdir /home/user/my-server/
exec node /home/user/my-server/server.js >> /var/log/my-server.log 2>&1

我得到的错误是:

...
apscallback 4
apscallback 5
apscallback 1 [Error: Socket unusable after connection. Hint: You may be using a certificate for the wrong environment]
... (REPEATS)
4

2 回答 2

2

您确实env NODE_ENV=production在 upstart 脚本中进行了设置(默认情况下,如果未设置此变量,则 APN 将与测试/沙盒环境相冲突,如果未设置该变量)。所以这将违背生产 APN。您是否拥有合适的证书来执行此操作(沙盒和生产的证书不同)?

于 2014-12-09T07:48:51.083 回答
0

你是暴发户脚本可能默认为root。也许 root 在其密钥环中没有正确的证书。尝试在你的新贵脚本中隐式设置用户。

您应该能够将用户设置为setuid <username>

于 2014-12-09T04:15:07.867 回答