コード例 #1
0
 public void onError(Exception error) {
   Logger.log(TAG, String.format(Locale.US, "Error: %s", error), error);
   setConnectionStatus(ConnectionStatus.DISCONNECTED);
   if (java.io.IOException.class.isAssignableFrom(error.getClass()) && reconnect) {
     scheduleReconnect();
   }
 }
コード例 #2
0
 public void onDisconnect(int code, String reason) {
   Logger.log(TAG, String.format(Locale.US, "Disconnect %d %s", code, reason));
   setConnectionStatus(ConnectionStatus.DISCONNECTED);
   notifyChannelsDisconnected();
   cancelHeartbeat();
   if (reconnect) scheduleReconnect();
 }
コード例 #3
0
 /** WebSocketClient.Listener methods for receiving status events from the socket */
 public void onConnect() {
   Logger.log(TAG, String.format("Connected"));
   setConnectionStatus(ConnectionStatus.CONNECTED);
   notifyChannelsConnected();
   heartbeatCount = 0; // reset heartbeat count
   scheduleHeartbeat();
   cancelReconnect();
   reconnectInterval = DEFAULT_RECONNECT_INTERVAL;
 }
コード例 #4
0
 public void connect() {
   // if we have channels, then connect, otherwise wait for a channel
   cancelReconnect();
   if (!isConnected() && !isConnecting() && !channels.isEmpty()) {
     Logger.log(TAG, String.format(Locale.US, "Connecting to %s", socketURI));
     setConnectionStatus(ConnectionStatus.CONNECTING);
     reconnect = true;
     socketClient.connect();
   }
 }
コード例 #5
0
 @Override
 public void onLog(Channel channel, int level, CharSequence message) {
   try {
     JSONObject log = new JSONObject();
     log.put(COMMAND_LOG, message);
     log.put(BUCKET_NAME_KEY, channel.getBucketName());
     log(level, log);
   } catch (JSONException e) {
     Logger.log(TAG, "Unable to send channel log message", e);
   }
 }
コード例 #6
0
 public void disconnect() {
   // disconnect the channel
   reconnect = false;
   cancelReconnect();
   if (isConnected()) {
     setConnectionStatus(ConnectionStatus.DISCONNECTING);
     Logger.log(TAG, "Disconnecting");
     // being told to disconnect so don't automatically reconnect
     socketClient.disconnect();
   }
 }
コード例 #7
0
  @Override
  public void log(int level, CharSequence message) {

    try {
      JSONObject log = new JSONObject();
      log.put(COMMAND_LOG, message.toString());
      log(level, log);
    } catch (JSONException e) {
      Logger.log(TAG, "Could not send log", e);
    }
  }
コード例 #8
0
  @Override
  public void onClose(Channel fromChannel) {
    // if we're allready disconnected we can ignore
    if (isDisconnected()) return;

    // check if all channels are disconnected and if so disconnect from the socket
    for (Channel channel : channels.values()) {
      if (channel.isStarted()) return;
    }
    Logger.log(
        TAG,
        String.format(Locale.US, "%s disconnect from socket", Thread.currentThread().getName()));
    disconnect();
  }
コード例 #9
0
 private void scheduleReconnect() {
   // check if we're not already trying to reconnect
   if (reconnectTimer != null) return;
   reconnectTimer = new Timer();
   // exponential backoff
   long retryIn = nextReconnectInterval();
   reconnectTimer.schedule(
       new TimerTask() {
         public void run() {
           connect();
         }
       },
       retryIn);
   Logger.log(String.format(Locale.US, "Retrying in %d", retryIn));
 }
コード例 #10
0
 public void onMessage(String message) {
   scheduleHeartbeat();
   int size = message.length();
   String[] parts = message.split(":", 2);
   ;
   if (parts[0].equals(COMMAND_HEARTBEAT)) {
     heartbeatCount = Integer.parseInt(parts[1]);
     return;
   } else if (parts[0].equals(COMMAND_LOG)) {
     logLevel = Integer.parseInt(parts[1]);
     return;
   }
   try {
     int channelId = Integer.parseInt(parts[0]);
     Channel channel = channels.get(channelId);
     channel.receiveMessage(parts[1]);
   } catch (NumberFormatException e) {
     Logger.log(TAG, String.format(Locale.US, "Unhandled message %s", parts[0]));
   }
 }