为什么我与Apple APNS的TCP连接挂起并强行断开连接

时间:2017-04-25 10:34:50

标签: c# sockets tcp apple-push-notifications pushsharp

我正在使用一个很棒的图书馆PushSharp来发送Apple& Android推送到许多客户端设备。 自上周以来,我们经常面临连接问题。在这些问题出现之前,我们的托管环境没有任何更新或任何更新。

问题出在ApnsConnection.cs

的这一行
System.IO.IOException: Unable to read data from the connection: 
An existing connection was forcibly closed by the remote host. ---> 
System.Net.Sockets.SocketException: An existing connection was forcibly 
closed by the remote host

有时TCP连接在这里停止响应。大约21分钟后,抛出以下异常:

13:11:33 | PushServiceV2 | APNS-Client[7]: Sending Batch ID=1, Count=21
13:11:33 | PushServiceV2 | APNS-Client[7]: Connecting (Batch ID=1)
13:11:33 | PushServiceV2 | APNS-Client[7]: client.ConnectAsync
13:11:33 | PushServiceV2 | APNS-Client[7]: client.Client.SetSocketOption
13:11:33 | PushServiceV2 | APNS-Client[7]: SetSocketKeepAliveValues
13:11:33 | PushServiceV2 | APNS-Client[7]: Configuration.SkipSsl:False
13:11:33 | PushServiceV2 | APNS-Client[7]: Create our ssl stream
13:11:33 | PushServiceV2 | APNS-Client[7]: stream.AuthenticateAsClient
13:32:03 | PushServiceV2 | APNS-CLIENT[7]: Send Batch Error: Batch ID=1, Error=System.IO.IOException:

从PushSharp进行日志记录显示以下内容:

//object with mp3 audio
var drake = {
  value: 3,
  name: 'Energy',
  artist: 'Drake',
  audio: 'https://www.w3schools.com/html/horse.mp3', //
  img: '<img style="width: 50%; margin-right: 25%; margin-left: 25%; margin-top: 10%;" src="http://www.getrichrapping.com/wp-content/uploads/2015/08/Drake-Energy.jpg">'
};


songValue = 1;

// plays and SHOULD pause a Song
function playSong(object) {

  //plays the song 
  if (songValue == 1) {
    var player = document.getElementById('song');
    var sourceMp3 = document.getElementById('sourceMp3');

    sourceMp3.src = object.audio;

    player.load(); //just start buffering (preload)
    player.play(); //start playing

    songValue = 2;

    // SHOULD pause the song when the functions is runned the second time
  } else if (songValue == 2) {
    console.log("Is it working ?"); // String to test if the "else if" gets runned
    var player = document.getElementById('song');

    if (!player.paused)
      player.pause();
    songValue = 1;

  }
};

正如您所看到的,“连接被远程主机强行关闭”需要21分钟。

要解决任何问题,我开始检查发送一批推送通知后我断开了连接。此外,我将批次之间的时间增加到5分钟。如果Apple断开我,因为我做了许多连接,我想这应该解决它。但事实并非如此。

  • 所有批次中有90%已成功发送和接收,因此证书应该是正确的;
  • 我会定期检查反馈服务并删除不活动的设备;
  • 我在不同的位置/网络上的不同开发机器上遇到同样的问题。

是否有任何关于如何进一步调查此问题的线索或提示?

非常感谢提前!

1 个答案:

答案 0 :(得分:0)

我认为我们只是限制了连接数量。我已阅读此文档:Troubleshooting Push Notifications并注意到这一行:

  

另一种可能性是你已经连接太多次APN   并暂时阻止了其他连接。原样   记录在本地和远程通知编程指南中,   开发人员应该打开一个连接并保持打开状态。如果一个   连接被反复打开和关闭,APN将其视为一个   拒绝服务攻击和阻止连接一段时间

我重写了我的实现和PushSharp的用法,以便尽可能少地创建和关闭TCP连接。它现在运行了几天没有任何问题。在日志记录中,我看到连接每天只通过apple断开一次,PushSharp很好地重新连接。

相关问题