Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Step 5b - Setting up a client to use token authentication
  • Loading branch information
piotr.kazmierczak committed Dec 2, 2016
1 parent e8412f5 commit ae636ad
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Tutorial/app/build.gradle
Expand Up @@ -22,4 +22,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:24.2.1'
/* add latest version of ably.io client library */
compile 'io.ably:ably-android:0.8.+'
/* add Volley library for simple network requests */
compile 'com.android.volley:volley:1.0.0'
}
2 changes: 2 additions & 0 deletions Tutorial/app/src/main/AndroidManifest.xml
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ably.io.tutorial">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
72 changes: 72 additions & 0 deletions Tutorial/app/src/main/java/ably/io/tutorial/ExampleActivity.java
Expand Up @@ -2,12 +2,84 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.RequestFuture;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import io.ably.lib.realtime.AblyRealtime;
import io.ably.lib.realtime.ConnectionState;
import io.ably.lib.realtime.ConnectionStateListener;
import io.ably.lib.rest.Auth;
import io.ably.lib.types.AblyException;
import io.ably.lib.types.ClientOptions;

public class ExampleActivity extends AppCompatActivity {

private AblyRealtime ablyRealtime;
private Auth.TokenRequest tokenRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_example);
try {
initAbly();
} catch (AblyException e) {
e.printStackTrace();
}
}

private void initAbly() throws AblyException {
ClientOptions clientOptions = new ClientOptions();
clientOptions.authCallback = new Auth.TokenCallback() {

@Override
public Object getTokenRequest(Auth.TokenParams tokenParams) throws AblyException {
/* issue synchronous query to obtain token request */
String httpAuthResponse = sendRequestToServer();
tokenRequest = new Gson().fromJson(httpAuthResponse, Auth.TokenRequest.class);
return tokenRequest;

}
};
ablyRealtime = new AblyRealtime(clientOptions);
ablyRealtime.connection.once(ConnectionState.connected, new ConnectionStateListener() {
@Override
public void onConnectionStateChanged(ConnectionStateChange connectionStateChange) {
/* always run UI work inside UI thread */
runOnUiThread(new Runnable() {
@Override
public void run() {
/* display message when connection is established */
Toast.makeText(getBaseContext(), "We're connected using the token request from the server /auth endpoint!", Toast.LENGTH_SHORT).show();
}
});
}
});
}

private String sendRequestToServer() {
RequestQueue queue = Volley.newRequestQueue(getBaseContext());
RequestFuture<String> future = RequestFuture.newFuture();
/* 10.0.2.2 is the IP address of localhost seen from emulator's perspective */
String url = "http://10.0.2.2:3000/auth";
StringRequest request = new StringRequest(url, future, future);
queue.add(request);
try {
/* return response from server with timeout set to 3 seconds */
return future.get(3, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException e) {
e.printStackTrace();
}
return null;
}

}
2 changes: 0 additions & 2 deletions Tutorial/build.gradle
Expand Up @@ -15,8 +15,6 @@ buildscript {
allprojects {
repositories {
jcenter()
/* add following repo for sub-dependency on all your modules */
maven { url "https://raw.github.com/paddybyers/Java-WebSocket/mvn-repo/" }
}
}

Expand Down
5 changes: 2 additions & 3 deletions server.js
Expand Up @@ -10,9 +10,8 @@ var rest = new Ably.Rest({ key: ApiKey });
const express = require('express'),
app = express();

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

/* Issue token requests to clients sending a request
to the /auth endpoint */
Expand Down

0 comments on commit ae636ad

Please sign in to comment.