private void connectWebSocket() { Log.d("FessBox", "Connect WebSocket"); try { // 192.168.1.38:19998 // 192.168.1.143:8001 connection.connect( "ws://192.168.1.143:8001", new WebSocketHandler() { @Override public void onOpen() { Intent intent = new Intent(App.LAUNCH_MAIN); sendBroadcast(intent); Log.d("FessBox", "Status: Connected"); } @Override public void onTextMessage(String payload) { if (listener != null) { listener.onMessage(payload); } } @Override public void onClose(int code, String reason) { Log.d("FessBox", "Connection lost."); } }); } catch (WebSocketException exception) { Log.d("FessBox", exception.toString()); } }
@Override public int onStartCommand(Intent intent, int flags, int startId) { // allow to use service when device is inactive WakeLock wakelock = ((PowerManager) getSystemService(POWER_SERVICE)) .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketServiceLock"); wakelock.acquire(); SLog.d(TAG, "onStartCommand"); if (intent != null) { SLog.d(TAG, intent.toUri(0)); } shutDown = false; // initialize connection if (connection == null || (!connection.isConnected() && !isConnecting)) { connectionWakeLock = ((PowerManager) getSystemService(POWER_SERVICE)) .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SocketService_clientLock"); connection = new WebSocketConnection(); try { isConnecting = true; connection.connect( String.format( Consts.SERVER_URL, Settings.getUserLogin(this), Settings.getUserPassword(this)), new WebSocketHandlerImpl()); // wake lock will be released in connection listener; } catch (WebSocketException e) { SLog.e(TAG, e.getMessage()); if (connectionWakeLock != null && connectionWakeLock.isHeld()) { connectionWakeLock.release(); } } // start shutting down } else if (intent != null) { if (ACTION_SHUT_DOWN.equals(intent.getAction())) { shutDown = true; if (connection.isConnected()) connection.disconnect(); } } wakelock.release(); return START_STICKY; }