|
2 | 2 |
|
3 | 3 | import android.os.Bundle;
|
4 | 4 | import android.support.v7.app.AppCompatActivity;
|
| 5 | +import android.widget.Toast; |
| 6 | + |
| 7 | +import com.android.volley.RequestQueue; |
| 8 | +import com.android.volley.toolbox.RequestFuture; |
| 9 | +import com.android.volley.toolbox.StringRequest; |
| 10 | +import com.android.volley.toolbox.Volley; |
| 11 | +import com.google.gson.Gson; |
| 12 | + |
| 13 | +import java.util.concurrent.ExecutionException; |
| 14 | +import java.util.concurrent.TimeUnit; |
| 15 | +import java.util.concurrent.TimeoutException; |
| 16 | + |
| 17 | +import io.ably.lib.realtime.AblyRealtime; |
| 18 | +import io.ably.lib.realtime.ConnectionState; |
| 19 | +import io.ably.lib.realtime.ConnectionStateListener; |
| 20 | +import io.ably.lib.rest.Auth; |
| 21 | +import io.ably.lib.types.AblyException; |
| 22 | +import io.ably.lib.types.ClientOptions; |
5 | 23 |
|
6 | 24 | public class ExampleActivity extends AppCompatActivity {
|
7 | 25 |
|
| 26 | + private AblyRealtime ablyRealtime; |
| 27 | + private Auth.TokenRequest tokenRequest; |
| 28 | + |
8 | 29 | @Override
|
9 | 30 | protected void onCreate(Bundle savedInstanceState) {
|
10 | 31 | super.onCreate(savedInstanceState);
|
11 | 32 | setContentView(R.layout.activity_example);
|
| 33 | + try { |
| 34 | + initAbly(); |
| 35 | + } catch (AblyException e) { |
| 36 | + e.printStackTrace(); |
| 37 | + } |
12 | 38 | }
|
| 39 | + |
| 40 | + private void initAbly() throws AblyException { |
| 41 | + ClientOptions clientOptions = new ClientOptions(); |
| 42 | + clientOptions.authCallback = new Auth.TokenCallback() { |
| 43 | + |
| 44 | + @Override |
| 45 | + public Object getTokenRequest(Auth.TokenParams tokenParams) throws AblyException { |
| 46 | + /* issue synchronous query to obtain token request */ |
| 47 | + String httpAuthResponse = sendRequestToServer(); |
| 48 | + tokenRequest = new Gson().fromJson(httpAuthResponse, Auth.TokenRequest.class); |
| 49 | + return tokenRequest; |
| 50 | + |
| 51 | + } |
| 52 | + }; |
| 53 | + ablyRealtime = new AblyRealtime(clientOptions); |
| 54 | + ablyRealtime.connection.once(ConnectionState.connected, new ConnectionStateListener() { |
| 55 | + @Override |
| 56 | + public void onConnectionStateChanged(ConnectionStateChange connectionStateChange) { |
| 57 | + /* always run UI work inside UI thread */ |
| 58 | + runOnUiThread(new Runnable() { |
| 59 | + @Override |
| 60 | + public void run() { |
| 61 | + /* display message when connection is established */ |
| 62 | + Toast.makeText(getBaseContext(), "We're connected using the token request from the server /auth endpoint!", Toast.LENGTH_SHORT).show(); |
| 63 | + } |
| 64 | + }); |
| 65 | + } |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + private String sendRequestToServer() { |
| 70 | + RequestQueue queue = Volley.newRequestQueue(getBaseContext()); |
| 71 | + RequestFuture<String> future = RequestFuture.newFuture(); |
| 72 | + /* 10.0.2.2 is the IP address of localhost seen from emulator's perspective */ |
| 73 | + String url = "http://10.0.2.2:3000/auth"; |
| 74 | + StringRequest request = new StringRequest(url, future, future); |
| 75 | + queue.add(request); |
| 76 | + try { |
| 77 | + /* return response from server with timeout set to 3 seconds */ |
| 78 | + return future.get(3, TimeUnit.SECONDS); |
| 79 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
13 | 85 | }
|
0 commit comments