Last active
November 2, 2019 12:19
-
-
Save jido2015/3aa757bfe36c07234d7ccd1edac751cc to your computer and use it in GitHub Desktop.
Service to emit and listen to server response every 5 seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class GlobalSocketWithEventBus extends Service { | |
| NetworkStateChange networkStateChange; | |
| Socket socket; | |
| Socket socket2; | |
| String token; | |
| GPSTracker gpsTracker; | |
| SharedPreferences prefToken; | |
| SharedPreferences prefUser; | |
| Handler handler = new Handler(); | |
| //Declare timer | |
| // CountDownTimer cTimer = null; | |
| //start timer function | |
| @Override | |
| public void onCreate() { | |
| super.onCreate(); | |
| try { | |
| socket = IO.socket("https://hubryde-trip-service.herokuapp.com/"); | |
| socket.connect(); | |
| socket2 = IO.socket("https://hubryde-request-service.herokuapp.com/"); | |
| socket2.connect(); | |
| Log.d("socket_", "Connected to Socket_Here"); | |
| //emitEventBus(); | |
| handler.post(sendData); | |
| } catch (URISyntaxException | IllegalArgumentException e) { | |
| Log.d("ServerTimeOut", String.valueOf(e)); | |
| } | |
| } | |
| @Override | |
| public int onStartCommand(Intent intent, int flags, int startId) { | |
| prefUser = getApplicationContext().getSharedPreferences("UserProfile", 0); | |
| networkStateChange = new NetworkStateChange(); | |
| prefToken = getApplicationContext().getSharedPreferences("MyPref", 0); | |
| token = prefToken.getString("TOKEN", null); | |
| assert token != null; | |
| try{ | |
| socket. | |
| on("allRequests"+prefUser.getString("EMAIL", null), args -> | |
| { | |
| freeMemory(); | |
| JSONObject result = (JSONObject) args[0]; | |
| Log.d("Server_Socket_splash", "Coming in"); | |
| EventBus.getDefault().post( | |
| new EventBusModel(result)); | |
| //result.remove((String) args[0]); | |
| }); | |
| } catch ( IllegalArgumentException e) { | |
| Log.d("ServerTimeOut", String.valueOf(e)); | |
| } | |
| return START_REDELIVER_INTENT; | |
| } | |
| @Override | |
| public IBinder onBind(Intent intent) { | |
| return null; | |
| } | |
| @Override | |
| public void onDestroy() { | |
| super.onDestroy(); | |
| stopSelf(); | |
| freeMemory(); | |
| } | |
| public void connectToWebSocket() { | |
| socket.connect(); | |
| socket2.connect(); | |
| } | |
| private final Runnable sendData = new Runnable(){ | |
| public void run(){ | |
| try { | |
| emitEventBus(); | |
| sendBusLocationToServer(); | |
| handler.removeCallbacks(sendData); | |
| //prepare and send the data here.. | |
| if (socket.connected()) { | |
| Log.d("socket_connected_test", "connected"); | |
| } else { | |
| connectToWebSocket(); | |
| } | |
| freeMemory(); | |
| handler.postDelayed(this, 5000); | |
| } catch (Exception e) { | |
| Log.d("ServerTimeOut", String.valueOf(e)); | |
| } | |
| } | |
| }; | |
| public void emitEventBus() { | |
| gpsTracker = new GPSTracker(getApplicationContext()); | |
| //REPEAT A METHORD AT SPECIFIC INTERVALS | |
| JSONObject obj = new JSONObject(); | |
| Log.d("ProfileDashBaord_socket","Connected to Socket_Here"); | |
| try { | |
| token = prefToken.getString("TOKEN", null); | |
| obj.put("token", token); | |
| obj.put("email", prefUser.getString("EMAIL", null)); | |
| obj.put("latitude", String.valueOf(gpsTracker.getLatitude())); | |
| obj.put("longitude", String.valueOf(gpsTracker.getLongitude())); | |
| } catch (JSONException e) { | |
| Log.d("ServerTimeOut", "Server Time out"); | |
| } | |
| Log.d("Board_emit",obj.toString()); | |
| // Receiving an object | |
| socket.emit("busRequests", obj); | |
| } | |
| public void sendBusLocationToServer() { | |
| gpsTracker = new GPSTracker(getApplicationContext()); | |
| try { | |
| JSONObject obj = new JSONObject(); | |
| obj.put("latitude", gpsTracker.getLatitude()); | |
| obj.put("longitude", gpsTracker.getLongitude()); | |
| obj.put("token", token); | |
| obj.put("bearing", gpsTracker.getBearing()); | |
| obj.put("phone", prefUser.getString("PHONE", null)); | |
| socket2.emit("busLocation", obj); | |
| //socket2.connect(); | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public void freeMemory(){ | |
| System.runFinalization(); | |
| Runtime.getRuntime().gc(); | |
| System.gc(); | |
| } | |
| @Override | |
| public void onTaskRemoved(Intent rootIntent){ | |
| Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass()); | |
| restartServiceIntent.setPackage(getPackageName()); | |
| PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT); | |
| AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); | |
| alarmService.set( | |
| AlarmManager.ELAPSED_REALTIME, | |
| SystemClock.elapsedRealtime() + 1000, | |
| restartServicePendingIntent); | |
| super.onTaskRemoved(rootIntent); | |
| } | |
| // public void startTimer() { | |
| // cTimer = new CountDownTimer(30000, 1000) { | |
| // public void onTick(long millisUntilFinished) { | |
| // } | |
| // public void onFinish() { | |
| // } | |
| // }; | |
| // cTimer.start(); | |
| // } | |
| // | |
| // | |
| // //cancel timer | |
| // void cancelTimer() { | |
| // if(cTimer!=null) | |
| // cTimer.cancel(); | |
| // } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment