Skip to content

Commit ae636ad

Browse files
author
piotr.kazmierczak
committed
Step 5b - Setting up a client to use token authentication
1 parent e8412f5 commit ae636ad

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

Tutorial/app/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ android {
2222
dependencies {
2323
compile fileTree(dir: 'libs', include: ['*.jar'])
2424
compile 'com.android.support:appcompat-v7:24.2.1'
25+
/* add latest version of ably.io client library */
26+
compile 'io.ably:ably-android:0.8.+'
27+
/* add Volley library for simple network requests */
28+
compile 'com.android.volley:volley:1.0.0'
2529
}

Tutorial/app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="ably.io.tutorial">
44

5+
<uses-permission android:name="android.permission.INTERNET" />
6+
57
<application
68
android:allowBackup="true"
79
android:icon="@mipmap/ic_launcher"

Tutorial/app/src/main/java/ably/io/tutorial/ExampleActivity.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,84 @@
22

33
import android.os.Bundle;
44
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;
523

624
public class ExampleActivity extends AppCompatActivity {
725

26+
private AblyRealtime ablyRealtime;
27+
private Auth.TokenRequest tokenRequest;
28+
829
@Override
930
protected void onCreate(Bundle savedInstanceState) {
1031
super.onCreate(savedInstanceState);
1132
setContentView(R.layout.activity_example);
33+
try {
34+
initAbly();
35+
} catch (AblyException e) {
36+
e.printStackTrace();
37+
}
1238
}
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+
1385
}

Tutorial/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ buildscript {
1515
allprojects {
1616
repositories {
1717
jcenter()
18-
/* add following repo for sub-dependency on all your modules */
19-
maven { url "https://raw.github.com/paddybyers/Java-WebSocket/mvn-repo/" }
2018
}
2119
}
2220

server.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@ var rest = new Ably.Rest({ key: ApiKey });
1010
const express = require('express'),
1111
app = express();
1212

13-
app.get('/', function (req, res) {
14-
res.send('Hello, I am a very simple server');
15-
});
13+
/* Server static content from the root path to keep things simple */
14+
app.use('/', express.static(__dirname));
1615

1716
/* Issue token requests to clients sending a request
1817
to the /auth endpoint */

0 commit comments

Comments
 (0)