Skip to content

Commit 3424acc

Browse files
Fetch token credentials before connecting
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 677fb78 commit 3424acc

5 files changed

Lines changed: 30 additions & 5 deletions

File tree

token_source/token_source_caching.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ bool cachingTokenSourceConnect() {
5555

5656
livekit::TokenRequestOptions request_options;
5757
request_options.participant_identity = "robot-a";
58+
const auto credentials = token_source->fetch(request_options).get();
59+
if (!credentials) {
60+
std::cerr << "Failed to fetch credentials: " << credentials.error().message << "\n";
61+
return false;
62+
}
5863

5964
livekit::Room room;
6065
ParticipantLogDelegate delegate;
6166
room.setDelegate(&delegate);
62-
if (!room.connect(*token_source, request_options, livekit::RoomOptions())) {
67+
if (!room.connect(credentials.value().server_url, credentials.value().participant_token, livekit::RoomOptions())) {
6368
std::cerr << "Failed to connect to room\n";
6469
return false;
6570
}

token_source/token_source_custom.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,16 @@ bool customTokenSourceConnect() {
7171
response.participant_token = token;
7272
return makeReadyFuture(TokenResult::success(std::move(response)));
7373
});
74+
const auto credentials = token_source->fetch(livekit::TokenRequestOptions()).get();
75+
if (!credentials) {
76+
std::cerr << "Failed to fetch credentials: " << credentials.error().message << "\n";
77+
return false;
78+
}
7479

7580
livekit::Room room;
7681
ParticipantLogDelegate delegate;
7782
room.setDelegate(&delegate);
78-
if (!room.connect(*token_source, livekit::TokenRequestOptions(), livekit::RoomOptions())) {
83+
if (!room.connect(credentials.value().server_url, credentials.value().participant_token, livekit::RoomOptions())) {
7984
std::cerr << "Failed to connect to room\n";
8085
return false;
8186
}

token_source/token_source_endpoint.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ bool endpointTokenSourceConnect() {
5555
// These options are sent to your endpoint, which embeds them into the JWT.
5656
livekit::TokenRequestOptions request_options;
5757
request_options.participant_identity = "robot-a";
58+
const auto credentials = token_source->fetch(request_options).get();
59+
if (!credentials) {
60+
std::cerr << "Failed to fetch credentials: " << credentials.error().message << "\n";
61+
return false;
62+
}
5863

5964
livekit::Room room;
6065
ParticipantLogDelegate delegate;
6166
room.setDelegate(&delegate);
62-
if (!room.connect(*token_source, request_options, livekit::RoomOptions())) {
67+
if (!room.connect(credentials.value().server_url, credentials.value().participant_token, livekit::RoomOptions())) {
6368
std::cerr << "Failed to connect to room\n";
6469
return false;
6570
}

token_source/token_source_literal.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,16 @@ bool literalTokenSourceConnect() {
4646
// Each fetch() returns these exact credentials; nothing is requested over the
4747
// network. Room and participant identity are encoded in the token.
4848
auto token_source = livekit::LiteralTokenSource::create(url, token);
49+
const auto credentials = token_source->fetch().get();
50+
if (!credentials) {
51+
std::cerr << "Failed to fetch credentials: " << credentials.error().message << "\n";
52+
return false;
53+
}
4954

5055
livekit::Room room;
5156
ParticipantLogDelegate delegate;
5257
room.setDelegate(&delegate);
53-
if (!room.connect(*token_source, livekit::RoomOptions())) {
58+
if (!room.connect(credentials.value().server_url, credentials.value().participant_token, livekit::RoomOptions())) {
5459
std::cerr << "Failed to connect to room\n";
5560
return false;
5661
}

token_source/token_source_sandbox.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,16 @@ bool sandboxTokenSourceConnect() {
5959
}
6060
std::cout << "Requesting sandbox token with agent dispatch: agent_name=" << *request_options.agent_name << "\n";
6161
}
62+
const auto credentials = token_source->fetch(request_options).get();
63+
if (!credentials) {
64+
std::cerr << "Failed to fetch credentials: " << credentials.error().message << "\n";
65+
return false;
66+
}
6267

6368
livekit::Room room;
6469
ParticipantLogDelegate delegate;
6570
room.setDelegate(&delegate);
66-
if (!room.connect(*token_source, request_options, livekit::RoomOptions())) {
71+
if (!room.connect(credentials.value().server_url, credentials.value().participant_token, livekit::RoomOptions())) {
6772
std::cerr << "Failed to connect to room\n";
6873
return false;
6974
}

0 commit comments

Comments
 (0)