Skip to content

Commit

Permalink
Add support for generic RequestResponse (#1221)
Browse files Browse the repository at this point in the history
* Add support for generic RequestResponse

* Create rare-hairs-deny.md

* Bump protocol version

* update lockfile
  • Loading branch information
lukasIO authored Aug 13, 2024
1 parent d9acb65 commit d53f530
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-hairs-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Add support for generic RequestResponse
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"size-limit": "size-limit"
},
"dependencies": {
"@livekit/protocol": "1.19.3",
"@livekit/protocol": "1.20.0",
"events": "^3.3.0",
"loglevel": "^1.8.0",
"sdp-transform": "^2.14.1",
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ClientInfo,
ConnectionQualityUpdate,
DisconnectReason,
ErrorResponse,
JoinResponse,
LeaveRequest,
LeaveRequest_Action,
Expand All @@ -13,6 +12,7 @@ import {
Ping,
ReconnectReason,
ReconnectResponse,
RequestResponse,
Room,
SessionDescription,
SignalRequest,
Expand Down Expand Up @@ -142,7 +142,7 @@ export class SignalClient {

onLeave?: (leave: LeaveRequest) => void;

onErrorResponse?: (error: ErrorResponse) => void;
onRequestResponse?: (response: RequestResponse) => void;

connectOptions?: ConnectOpts;

Expand Down Expand Up @@ -739,9 +739,9 @@ export class SignalClient {
this.rtt = Date.now() - Number.parseInt(msg.value.lastPingTimestamp.toString());
this.resetPingTimeout();
pingHandled = true;
} else if (msg.case === 'errorResponse') {
if (this.onErrorResponse) {
this.onErrorResponse(msg.value);
} else if (msg.case === 'requestResponse') {
if (this.onRequestResponse) {
this.onRequestResponse(msg.value);
}
} else {
this.log.debug('unsupported message', { ...this.logContext, msgCase: msg.case });
Expand Down
7 changes: 4 additions & 3 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {
DataPacket,
DataPacket_Kind,
DisconnectReason,
ErrorResponse,
type JoinResponse,
type LeaveRequest,
LeaveRequest_Action,
ParticipantInfo,
ReconnectReason,
type ReconnectResponse,
RequestResponse,
Room as RoomModel,
SignalTarget,
SpeakerInfo,
Expand Down Expand Up @@ -194,7 +194,8 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
this.emit(EngineEvent.SubscriptionPermissionUpdate, update);
this.client.onSpeakersChanged = (update) => this.emit(EngineEvent.SpeakersChanged, update);
this.client.onStreamStateUpdate = (update) => this.emit(EngineEvent.StreamStateChanged, update);
this.client.onErrorResponse = (error) => this.emit(EngineEvent.SignalRequestError, error);
this.client.onRequestResponse = (response) =>
this.emit(EngineEvent.SignalRequestResponse, response);
}

/** @internal */
Expand Down Expand Up @@ -1414,7 +1415,7 @@ export type EngineEventCallbacks = {
localTrackUnpublished: (unpublishedResponse: TrackUnpublishedResponse) => void;
remoteMute: (trackSid: string, muted: boolean) => void;
offline: () => void;
signalRequestError: (error: ErrorResponse) => void;
signalRequestResponse: (response: RequestResponse) => void;
};

function supportOptionalDatachannel(protocol: number | undefined): boolean {
Expand Down
10 changes: 7 additions & 3 deletions src/room/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ErrorResponse_Reason } from '@livekit/protocol';
import { RequestResponse_Reason } from '@livekit/protocol';

export class LivekitError extends Error {
code: number;
Expand Down Expand Up @@ -65,10 +65,14 @@ export class PublishDataError extends LivekitError {
}
}

export type RequestErrorReason =
| Exclude<RequestResponse_Reason, RequestResponse_Reason.OK>
| 'TimeoutError';

export class SignalRequestError extends LivekitError {
reason: ErrorResponse_Reason;
reason: RequestErrorReason;

constructor(message: string, reason: ErrorResponse_Reason = ErrorResponse_Reason.UNKNOWN) {
constructor(message: string, reason: RequestErrorReason) {
super(15, message);
this.reason = reason;
}
Expand Down
2 changes: 1 addition & 1 deletion src/room/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ export enum EngineEvent {
SubscribedQualityUpdate = 'subscribedQualityUpdate',
LocalTrackUnpublished = 'localTrackUnpublished',
Offline = 'offline',
SignalRequestError = 'signalRequestError',
SignalRequestResponse = 'signalRequestResponse',
}

export enum TrackEvent {
Expand Down
21 changes: 13 additions & 8 deletions src/room/participant/LocalParticipant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
DataPacket,
DataPacket_Kind,
Encryption_Type,
ErrorResponse,
ParticipantInfo,
ParticipantPermission,
RequestResponse,
RequestResponse_Reason,
SimulcastCodec,
SubscribedQualityUpdate,
TrackUnpublishedResponse,
Expand Down Expand Up @@ -177,7 +178,7 @@ export default class LocalParticipant extends Participant {
.on(EngineEvent.LocalTrackUnpublished, this.handleLocalTrackUnpublished)
.on(EngineEvent.SubscribedQualityUpdate, this.handleSubscribedQualityUpdate)
.on(EngineEvent.Disconnected, this.handleDisconnected)
.on(EngineEvent.SignalRequestError, this.handleSignalRequestError);
.on(EngineEvent.SignalRequestResponse, this.handleSignalRequestResponse);
}

private handleReconnecting = () => {
Expand All @@ -200,11 +201,13 @@ export default class LocalParticipant extends Participant {
}
};

private handleSignalRequestError = (error: ErrorResponse) => {
const { requestId, reason, message } = error;
const failedRequest = this.pendingSignalRequests.get(requestId);
if (failedRequest) {
failedRequest.reject(new SignalRequestError(message, reason));
private handleSignalRequestResponse = (response: RequestResponse) => {
const { requestId, reason, message } = response;
const targetRequest = this.pendingSignalRequests.get(requestId);
if (targetRequest) {
if (reason !== RequestResponse_Reason.OK) {
targetRequest.reject(new SignalRequestError(message, reason));
}
this.pendingSignalRequests.delete(requestId);
}
};
Expand Down Expand Up @@ -278,7 +281,9 @@ export default class LocalParticipant extends Participant {
}
await sleep(50);
}
reject(new SignalRequestError('Request to update local metadata timed out'));
reject(
new SignalRequestError('Request to update local metadata timed out', 'TimeoutError'),
);
} catch (e: any) {
if (e instanceof Error) reject(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { version as v } from '../package.json';

export const version = v;
export const protocolVersion = 13;
export const protocolVersion = 15;

0 comments on commit d53f530

Please sign in to comment.