@Override public void sendServerPing() { if (mPingID != null) {// 此时说明上一次ping服务器还未回应,直接返回,直到连接超时 L.d("Ping: requested, but still waiting for " + mPingID); return; // a ping is still on its way } Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(PreferenceUtils.getPrefString(mService, PreferenceConstants.Server, PreferenceConstants.GMAIL_SERVER)); mPingID = ping.getPacketID();// 此id其实是随机生成,但是唯一的 mPingTimestamp = System.currentTimeMillis(); L.d("Ping: sending ping " + mPingID); mXMPPConnection.sendPacket(ping);// 发送ping消息 // register ping timeout handler: PACKET_TIMEOUT(30s) + 3s ((AlarmManager) mService.getSystemService(Context.ALARM_SERVICE)).set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + PACKET_TIMEOUT + 3000, mPongTimeoutAlarmPendIntent);// 此时需要启动超时判断的闹钟了,时间间隔为30+3秒 }
@Override public void sendServerPing() { if (mPingID != null) {// 此时说明上一次ping服务器还未回应,直接返回,直到连接超时 AppLogger.d("Ping: requested, but still waiting for " + mPingID); return; // a ping is still on its way } Ping ping = new Ping(); ping.setType(Type.GET); ping.setTo(PreferenceUtils.getPrefString(mService, PreferenceConstants.Server, PreferenceConstants.GMAIL_SERVER)); mPingID = ping.getPacketID();// 此id其实是随机生成,但是唯一的 mPingTimestamp = System.currentTimeMillis(); AppLogger.d("Ping: sending ping " + mPingID); mXMPPConnection.sendPacket(ping);// 发送ping消息 // register ping timeout handler: PACKET_TIMEOUT(30s) + 3s ((AlarmManager) mService.getSystemService(Context.ALARM_SERVICE)).set( AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + PACKET_TIMEOUT + 3000, mPongTimeoutAlarmPendIntent);// 此时需要启动超时判断的闹钟了,时间间隔为30+3秒 }
@Override public Ping parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException { // No need to use the ping constructor with arguments. IQ will already // have filled out all relevant fields ('from', 'to', 'id'). return new Ping(); }
private PingManager(final Connection connection) { this.connection = connection; instances.put(connection, this); ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection); sdm.addFeature(NAMESPACE); PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class); connection.addPacketListener(new PacketListener() { /** * Sends a Pong for every Ping */ public void processPacket(Packet packet) { if (pingMinDelta > 0) { // Ping flood protection enabled long currentMillies = System.currentTimeMillis(); long delta = currentMillies - lastPingStamp; lastPingStamp = currentMillies; if (delta < pingMinDelta) { return; } } Pong pong = new Pong((Ping)packet); connection.sendPacket(pong); } } , pingPacketFilter); connection.addConnectionListener(new ConnectionListener() { @Override public void connectionClosed() { maybeStopPingServerTask(); } @Override public void connectionClosedOnError(Exception arg0) { maybeStopPingServerTask(); } @Override public void reconnectionSuccessful() { maybeSchedulePingServerTask(); } @Override public void reconnectingIn(int seconds) { } @Override public void reconnectionFailed(Exception e) { } }); maybeSchedulePingServerTask(); }
public IQ parseIQ(XmlPullParser parser) throws Exception { // No need to use the ping constructor with arguments. IQ will already // have filled out all relevant fields ('from', 'to', 'id'). return new Ping(); }
private void init() { periodicPingExecutorService = new ScheduledThreadPoolExecutor(1); PacketFilter pingPacketFilter = new PacketTypeFilter(Ping.class); connection.addPacketListener(new PacketListener() { /** * Sends a Pong for every Ping */ public void processPacket(Packet packet) { if (pingMinDelta > 0) { // Ping flood protection enabled long currentMillies = System.currentTimeMillis(); long delta = currentMillies - lastPingStamp; lastPingStamp = currentMillies; if (delta < pingMinDelta) { return; } } Pong pong = new Pong((Ping)packet); connection.sendPacket(pong); } } , pingPacketFilter); connection.addConnectionListener(new ConnectionListener() { @Override public void connectionClosed() { maybeStopPingServerTask(); } @Override public void connectionClosedOnError(Exception arg0) { maybeStopPingServerTask(); } @Override public void reconnectionSuccessful() { maybeSchedulePingServerTask(); } @Override public void reconnectingIn(int seconds) { } @Override public void reconnectionFailed(Exception e) { } }); instances.put(connection, this); maybeSchedulePingServerTask(); }
/** * Query the specified entity to see if it supports the Ping protocol (XEP-0199) * * @param jid The id of the entity the query is being sent to * @return true if it supports ping, false otherwise. * @throws XMPPErrorException An XMPP related error occurred during the request * @throws NoResponseException if there was no response from the jid. * @throws NotConnectedException */ public boolean isPingSupported(String jid) throws NoResponseException, XMPPErrorException, NotConnectedException { return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, Ping.NAMESPACE); }