The relevant code in this article can be viewed in the demoAPI
RoomActivity
class of the Demo project.
This part is server logic. In actual development, it should not be implemented on the client side.
The client should make a request to the developer's own back-end server to obtain the content.
The sdkToken of the new user used in this document starts with NETLESS and cannot use the
v4
API. It needs to be created on the server side according to server.
To create a room/get a room, you need to use sdkToken to interact with the SDK back-end server.
This Token is the credential for the SDK back-end server to communicate with the developer's back-end business server. With this token, the SDK back-end server will think that this is an operation performed by the developer.
In the example, for the convenience of demonstration, the operation of creating a room/obtaining a room token is written in the client. In actual business, in order to prevent someone from decompiling the client code and obtaining the SDKToken, the token should not be exposed on any client.
The Token should be in the developer's business server code or configuration item.
Make a request to the sdk business server
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
public class DemoAPI {
public void createRoom(String name, int limit, Callback callback) {
Map<String, Object> roomSpec = new HashMap<>();
roomSpec.put("name", name);
roomSpec.put("limit", limit);
RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
Request request = new Request.Builder()
.url(host + "/room?token=" + sdkToken)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
}
}
From the response of the sdk server, read uuid and roomToken.
public class RoomActivity {
demoAPI.createRoom("sdk demo", 100, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
alert("Network request error", e.toString());
}
@Override
public void onResponse(Call call, Response response) {
try {
if (response.code() == 200) {
JsonObject room = gson.fromJson(response.body().string(), JsonObject.class);
String uuid = room.getAsJsonObject("msg").getAsJsonObject("room").get("uuid").getAsString();
String roomToken = room.getAsJsonObject("msg").get("roomToken").getAsString();
if (whiteBroadView.getEnv() == Environment.dev) {
joinRoom(TEST_UUID, TEST_ROOM_TOKEN);
} else {
joinRoom(uuid, roomToken);
}
} else {
alert("Network request error", response.body().string());
}
} catch (Throwable e) {
alert("Failed to create room", e.toString());
}
}
});
}
public class demoAPI {
public void getRoomToken(String uuid, Callback callback) {
Map<String, Object> roomSpec = new HashMap<>();
RequestBody body = RequestBody.create(JSON, gson.toJson(roomSpec));
Request request = new Request.Builder()
.url(host + "/room/join?uuid=" + uuid + "&token=" + sdkToken)
.post(body)
.build();
Call call = client.newCall(request);
call.enqueue(callback);
}
}
public class RoomActivity {
private void getRoomToken(final String uuid) {
demoAPI.getRoomToken(uuid, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
alert("The request to get the room token failed", e.toString());
}
@Override
public void onResponse(Call call, Response response) {
try {
if (response.code() == 200) {
JsonObject room = gson.fromJson(response.body().string(), JsonObject.class);
String roomToken = room.getAsJsonObject("msg").get("roomToken").getAsString();
if (whiteBroadView.getEnv() == Environment.dev) {
joinRoom(TEST_UUID, TEST_ROOM_TOKEN);
} else {
joinRoom(uuid, roomToken);
}
} else {
alert("Failed to get room token", response.body().string());
}
} catch (Throwable e) {
alert("Failed to obtain room token", e.toString());
}
}
});
}
}