Advertisement
Leonard_M

WebSockets

Jan 18th, 2021
2,720
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. package com.example.template;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.Toast;
  11.  
  12. import com.example.template.database.CrudEntityDatabase;
  13. import com.example.template.domain.Exam;
  14. import com.google.gson.Gson;
  15.  
  16. import java.net.URI;
  17. import java.net.URISyntaxException;
  18.  
  19. import tech.gusavila92.websocketclient.WebSocketClient;
  20.  
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23.     private WebSocketClient webSocketClient;
  24.  
  25.     Button crudActivityButton;
  26.     Button multipleAdapterActivity;
  27.     Button statsButton;
  28.  
  29.     @Override
  30.     protected void onCreate(Bundle savedInstanceState) {
  31.         super.onCreate(savedInstanceState);
  32.         setContentView(R.layout.activity_main);
  33.  
  34.         //todo: fix this
  35.         CrudEntityDatabase.getAppDatabase(this);
  36.  
  37.  
  38.         crudActivityButton = findViewById(R.id.crudActivityButton);
  39.         multipleAdapterActivity = findViewById(R.id.multipleAdapterActivity);
  40.         statsButton = findViewById(R.id.statsButton);
  41.  
  42.         crudActivityButton.setOnClickListener(new View.OnClickListener() {
  43.             @Override
  44.             public void onClick(View v) {
  45.                 Intent intent = new Intent(MainActivity.this, CrudActivity.class);
  46.                 startActivity(intent);
  47.             }
  48.         });
  49.  
  50.         multipleAdapterActivity.setOnClickListener(new View.OnClickListener() {
  51.             @Override
  52.             public void onClick(View v) {
  53.                 Intent intent = new Intent(MainActivity.this, MultipleAdapterActivity.class);
  54.                 startActivity(intent);
  55.             }
  56.         });
  57.  
  58.         statsButton.setOnClickListener(new View.OnClickListener() {
  59.             @Override
  60.             public void onClick(View v) {
  61.                 Intent intent = new Intent(MainActivity.this, StatsActivity.class);
  62.                 startActivity(intent);
  63.             }
  64.         });
  65.  
  66.         createWebSocketClient();
  67.     }
  68.  
  69.     private void createWebSocketClient() {
  70.         URI uri;
  71.         try {
  72.             // Connect to local host
  73.             uri = new URI("ws://192.168.0.122:8080/");
  74.         }
  75.         catch (URISyntaxException e) {
  76.             e.printStackTrace();
  77.             return;
  78.         }
  79.         webSocketClient = new WebSocketClient(uri) {
  80.  
  81.             @Override
  82.             public void onOpen() {
  83.  
  84.             }
  85.  
  86.             @Override
  87.             public void onTextReceived(String object) {
  88.                 Log.i("WebSocket", "Message received");
  89.                 Gson gson = new Gson();
  90.                 Exam exam = gson.fromJson(object, Exam.class);
  91.  
  92.                 runOnUiThread(new Runnable() {
  93.                     @Override
  94.                     public void run() {
  95.                         try{
  96.                             Toast.makeText(MainActivity.this, exam.toString(), Toast.LENGTH_SHORT).show();
  97.                         } catch (Exception e){
  98.                             e.printStackTrace();
  99.                         }
  100.                     }
  101.                 });
  102.             }
  103.  
  104.             @Override
  105.             public void onBinaryReceived(byte[] data) {
  106.  
  107.             }
  108.  
  109.             @Override
  110.             public void onPingReceived(byte[] data) {
  111.  
  112.             }
  113.  
  114.             @Override
  115.             public void onPongReceived(byte[] data) {
  116.  
  117.             }
  118.  
  119.             @Override
  120.             public void onException(Exception e) {
  121.  
  122.             }
  123.  
  124.             @Override
  125.             public void onCloseReceived() {
  126.  
  127.             }
  128.  
  129.  
  130.         };
  131.         webSocketClient.setConnectTimeout(10000);
  132.         webSocketClient.setReadTimeout(60000);
  133.         webSocketClient.enableAutomaticReconnection(5000);
  134.         webSocketClient.connect();
  135.     }
  136.  
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement