/** Method to send message to web socket server */ private boolean sendMessageToServer(String message) { if (client != null && client.isConnected()) { Log.d(TAG + ":sendMessageToServer", "client connected"); client.send(message); return true; } else { startWebSocketClient(); return false; } }
@Override protected void onDestroy() { super.onDestroy(); stopLocationUpdates(); if (mLocalBroadcastManager != null) { mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver); } if (client != null & client.isConnected()) { client.disconnect(); clientListener = null; client = null; } }
private synchronized void sendHearbeat() { heartbeatCount++; String command = String.format(Locale.US, "%s:%d", COMMAND_HEARTBEAT, heartbeatCount); if (!isConnected()) return; socketClient.send(command); }
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(); } }
protected void log(int level, JSONObject log) { // no logging if disabled if (logLevel == ChannelProvider.LOG_DISABLED) return; if (level > logLevel) return; if (!isConnected()) return; socketClient.send(String.format(LOG_FORMAT, COMMAND_LOG, log)); }
/** Channel.OnMessageListener event listener */ @Override public void onMessage(Channel.MessageEvent event) { Channel channel = (Channel) event.getSource(); Integer channelId = channelIndex.get(channel); // Prefix the message with the correct channel id String message = String.format(Locale.US, "%d:%s", channelId, event.getMessage()); if (isConnected()) { socketClient.send(message); } }
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 run() { try { mAccessTokens = requestAccessTokens(mUserId, mAuthToken); } catch (JSONException e) { e.printStackTrace(); } if (mAccessTokens == null) { return; } mWebSocketClient = new WebSocketClient(URI.create(HATCHET_URL), TomahawkService.this, null); mWebSocketClient.connect(); }
/** Called when the websocket client has connected. */ @Override public void onConnect() { Log.d(TAG, "Tomahawk websocket connected."); /** For testing we will attempt to register. */ AccessToken token = mAccessTokens.get(0); JSONObject register = new JSONObject(); try { register.put("command", "register"); register.put("hostname", token.localhost); register.put("port", token.port); register.put("accesstoken", token.token); register.put("username", mUserId); register.put("dbid", "nil"); } catch (JSONException e) { e.printStackTrace(); } mWebSocketClient.send(register.toString()); }
private void startWebSocketClient() { client = new WebSocketClient(URI.create(WsConfig.URL_WEBSOCKET), clientListener, null); client.connect(); }