【问题标题】:APNS - notifications push ios: Connection reset by peer PHPAPNS - 通知推送 ios:对等 PHP 重置连接
【发布时间】:2016-06-25 13:50:58
【问题描述】:

我的推送通知工作正常。但有些时候,它从无到有开始给出错误:

stream_socket_client():SSL:对等方重置连接

奇怪的是我不需要做任何事情来解决它,而是等待。 一段时间后,它又开始工作了。

我知道这是许多问题的重复,例如: notifications-push-ios-connection-reset-by-peer 但是没有一个能解决我的问题。

我正在使用 PHP stream_socket_client 来生成套接字连接

使用的代码是:

 <?php
ini_set('display_errors','On'); 
error_reporting(E_ALL);
$deviceToken= 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';      
$passphrase = ' ';
$message = 'my first notification';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,
    $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
    echo 'Message not delivered' . PHP_EOL;
else
    echo 'Message successfully delivered' . PHP_EOL;
// Close the connection
}

【问题讨论】:

    标签: php ios apple-push-notifications


    【解决方案1】:

    我无法真正指出它的主要原因。

    但请确保,您没有做以下任何错误:

    • 不要同时建立多个连接。在传递推送通知后重用相同的连接或关闭连接。实际上,服务器对最大并行连接数有限制,一旦达到阈值,这可能会给您带来麻烦。 Apple 还建议保持连接打开,除非您知道它会处于空闲状态。

    在多个通知中保持与 APN 的连接处于打开状态; 不要反复打开和关闭连接。 APNs 治疗快速 连接和断开作为拒绝服务攻击。你应该 保持连接打开,除非您知道它将空闲一段时间 延长时间——例如,如果您只向 您的用户每天一次,可以每天使用一个新连接。

    • 不要将开发者资料令牌发送到 LIVE APNS。 将分发和开发应用程序令牌分开。如果您尝试将沙盒令牌发送到 LIVE APNS,反之亦然,这可能会导致错误。

    【讨论】:

    • 我想,我可能没有正确关闭连接。我会验证的。
    【解决方案2】:

    在这个问题上挣扎了一段时间。就我而言,结果是:

    a) 和过期的 APNS 证书。只有一小部分错误解释说它是一个过期的证书。大多数是“由对等方重置连接”。

    b) 损坏的 APNS 证书。只有在重新生成证书后它才开始工作。在那之前,我再次遇到了臭名昭著的“对等重置连接”。

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多