APNS-通知推送ios:对等PHP重置连接 [英] APNS - notifications push ios: Connection reset by peer PHP

查看:154
本文介绍了APNS-通知推送ios:对等PHP重置连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Push Notifications工作正常.但是有时候,从无处开始会出现错误:

I have had Push Notifications working fine. But some of the times, from nowhere it starts giving error:

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

stream_socket_client(): SSL: Connection reset by peer

奇怪的是我不必做任何事情来解决它,而是等待. 一段时间后,它将重新开始工作.

Weird thing is i don't have to do anything to resolve it but wait. After sometime, it starts working back again.

我知道这是许多问题的重复,例如: notifications-push-ios-connection-reset-by-peer 但是它们都不能解决我的问题.

I know it is a duplicate of many questions like: notifications-push-ios-connection-reset-by-peer But none of them solves my problem.

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

I am using PHP stream_socket_client to generate the socket connection

正在使用的代码是:

 <?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
}

推荐答案

我无法真正指出主要原因.

I can't really put my finger on main reason for it.

但是请确保您没有做错以下任何事情:

But please make sure, you are not doing any of the below things wrong:

  • 不要并行建立许多连接.传递推送通知"后,请重用同一连接或关闭该连接.实际上,服务器有最大并行连接数限制,一旦达到阈值,这可能会给您带来麻烦.另外,苹果公司建议除非您知道连接将处于空闲状态,否则将其保持打开状态.
  • Don't make many connections in parallel. Either reuse the same connection or close the connection after delivering Push Notifications. Actually, servers have a limit for maximum number of parallel connections, which might leave you in trouble, once you reach threshold. Also Apple suggests leave a connection open unless you know it will be idle.

使您与APN的连接在多个通知中保持打开状态; 不要反复打开和关闭连接. APNs治疗迅速 连接和断开作为拒绝服务攻击.你应该 除非您知道某个连接将闲置,否则请保持连接处于打开状态 较长的时间段-例如,如果您仅将通知发送给 您的用户每天可以使用每天一次的新连接.

Keep your connections with APNs open across multiple notifications; don’t repeatedly open and close connections. APNs treats rapid connection and disconnection as a denial-of-service attack. You should leave a connection open unless you know it will be idle for an extended period of time—for example, if you only send notifications to your users once a day it is ok to use a new connection each day.

  • 请勿将开发者配置文件令牌发送到LIVE APNS.将分发和开发应用程序令牌分开.如果您尝试将沙盒令牌发送到LIVE APNS,反之亦然,则可能会导致错误.
    • Don't send out developer profile tokens to LIVE APNS. Keep distribution and development app tokens separate. It could result in error, if you try to send sandbox tokens to LIVE APNS or vice versa.
    • 这篇关于APNS-通知推送ios:对等PHP重置连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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