@Override public void run() { if (TextUtil.checkUUID(mUuid)) { ServerMessage msg = mPool.getMsg(mUuid); long now = System.currentTimeMillis(); if (msg != null && ((now - msg.getLastHeartBeat()) >= mHeartBeatInterval)) { try { if (mSocket != null && mSocket.isConnected()) { OutputStream os = mSocket.getOutputStream(); os.write(Message.createHeatBeatMsg(mUuid, msg.haveUnreadMsg())); os.flush(); msg.setLastHeartBeat(now); } } catch (IOException e) { LogUtil.logE("IOException", e); } } } /* long lastRun = System.currentTimeMillis(); long now; while (mServiceRunning) { now = System.currentTimeMillis(); if ((now - lastRun) < (mHeartBeatInterval*1000)) { try { Thread.sleep((mHeartBeatInterval*1000) - (now - lastRun)); } catch (InterruptedException e) { LogUtil.logE("InterruptedException", e); } } else if ((now - lastRun) > (mHeartBeatInterval*1000)) { LogUtil.logI("HeartBeatWork Overload "); } lastRun = now; } */ }
public HeartBeatWorker(String uuid, Socket socket) { mUuid = uuid; mSocket = socket; mPool = MessagePool.getInstance(); }