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(); } }
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(); }
/** 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; }
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(); } }
@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); } }
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(); } }
@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); } }
@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(); }
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)); }
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])); } }