diff --git a/packages/sdk/src/base.ts b/packages/sdk/src/base.ts index a8b5173b..c8e4fb86 100644 --- a/packages/sdk/src/base.ts +++ b/packages/sdk/src/base.ts @@ -1,4 +1,16 @@ import { + SIGNALING_MESSAGE_TYPE_ANSWER, + SIGNALING_MESSAGE_TYPE_CANDIDATE, + SIGNALING_MESSAGE_TYPE_NOTIFY, + SIGNALING_MESSAGE_TYPE_OFFER, + SIGNALING_MESSAGE_TYPE_PING, + SIGNALING_MESSAGE_TYPE_PONG, + SIGNALING_MESSAGE_TYPE_PUSH, + SIGNALING_MESSAGE_TYPE_REDIRECT, + SIGNALING_MESSAGE_TYPE_RE_ANSWER, + SIGNALING_MESSAGE_TYPE_RE_OFFER, + SIGNALING_MESSAGE_TYPE_SWITCHED, + SIGNALING_MESSAGE_TYPE_UPDATE, SORA_ROLE_RECVONLY, SORA_ROLE_SENDONLY, SORA_ROLE_SENDRECV, @@ -1194,32 +1206,32 @@ export default class ConnectionBase { throw new Error('Received invalid signaling data') } const message = JSON.parse(event.data) as WebSocketSignalingMessage - if (message.type === 'offer') { + if (message.type === SIGNALING_MESSAGE_TYPE_OFFER) { this.writeWebSocketSignalingLog('onmessage-offer', message) this.signalingOnMessageTypeOffer(message) this.connectedSignalingUrl = ws.url resolve(message) - } else if (message.type === 'update') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_UPDATE) { this.writeWebSocketSignalingLog('onmessage-update', message) await this.signalingOnMessageTypeUpdate(message) - } else if (message.type === 're-offer') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_RE_OFFER) { this.writeWebSocketSignalingLog('onmessage-re-offer', message) await this.signalingOnMessageTypeReOffer(message) - } else if (message.type === 'ping') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_PING) { await this.signalingOnMessageTypePing(message) - } else if (message.type === 'push') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_PUSH) { this.callbacks.push(message, TRANSPORT_TYPE_WEBSOCKET) - } else if (message.type === 'notify') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_NOTIFY) { if (message.event_type === 'connection.created') { this.writeWebSocketTimelineLog('notify-connection.created', message) } else if (message.event_type === 'connection.destroyed') { this.writeWebSocketTimelineLog('notify-connection.destroyed', message) } this.signalingOnMessageTypeNotify(message, TRANSPORT_TYPE_WEBSOCKET) - } else if (message.type === 'switched') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_SWITCHED) { this.writeWebSocketSignalingLog('onmessage-switched', message) this.signalingOnMessageTypeSwitched(message) - } else if (message.type === 'redirect') { + } else if (message.type === SIGNALING_MESSAGE_TYPE_REDIRECT) { this.writeWebSocketSignalingLog('onmessage-redirect', message) try { const redirectMessage = await this.signalingOnMessageTypeRedirect(message) @@ -1325,7 +1337,7 @@ export default class ConnectionBase { const sdp = this.processOfferSdp(message.sdp) const sessionDescription = new RTCSessionDescription({ - type: 'offer', + type: SIGNALING_MESSAGE_TYPE_OFFER, sdp, }) await this.pc.setRemoteDescription(sessionDescription) @@ -1420,7 +1432,7 @@ export default class ConnectionBase { if (this.pc && this.ws && this.pc.localDescription) { this.trace('ANSWER SDP', this.pc.localDescription.sdp) const sdp = this.pc.localDescription.sdp - const message = { type: 'answer', sdp } + const message = { type: SIGNALING_MESSAGE_TYPE_ANSWER, sdp } this.ws.send(JSON.stringify(message)) this.writeWebSocketSignalingLog('send-answer', message) } @@ -1456,7 +1468,9 @@ export default class ConnectionBase { resolve() } else { const candidate = event.candidate.toJSON() - const message = Object.assign(candidate, { type: 'candidate' }) as { + const message = Object.assign(candidate, { + type: SIGNALING_MESSAGE_TYPE_CANDIDATE, + }) as { type: string [key: string]: unknown } @@ -1817,9 +1831,9 @@ export default class ConnectionBase { */ private async sendUpdateAnswer(): Promise { if (this.pc && this.ws && this.pc.localDescription) { - this.trace('ANSWER SDP', this.pc.localDescription.sdp) + this.trace('UPDATE ANSWER SDP', this.pc.localDescription.sdp) await this.sendSignalingMessage({ - type: 'update', + type: SIGNALING_MESSAGE_TYPE_UPDATE, sdp: this.pc.localDescription.sdp, }) } @@ -1827,12 +1841,13 @@ export default class ConnectionBase { /** * シグナリングサーバーに type re-answer を投げるメソッド + * @deprecated このメソッドは非推奨です。将来のバージョンで削除される可能性があります。 */ private async sendReAnswer(): Promise { if (this.pc?.localDescription) { this.trace('RE ANSWER SDP', this.pc.localDescription.sdp) await this.sendSignalingMessage({ - type: 're-answer', + type: SIGNALING_MESSAGE_TYPE_RE_ANSWER, sdp: this.pc.localDescription.sdp, }) } @@ -1880,8 +1895,8 @@ export default class ConnectionBase { * @param message - type ping メッセージ */ private async signalingOnMessageTypePing(message: SignalingPingMessage): Promise { - const pongMessage: { type: 'pong'; stats?: RTCStatsReport[] } = { - type: 'pong', + const pongMessage: { type: typeof SIGNALING_MESSAGE_TYPE_PONG; stats?: RTCStatsReport[] } = { + type: SIGNALING_MESSAGE_TYPE_PONG, } if (message.stats) { const stats = await this.getStats() diff --git a/packages/sdk/src/constants.ts b/packages/sdk/src/constants.ts index 66e3a4a2..ecaa5d75 100644 --- a/packages/sdk/src/constants.ts +++ b/packages/sdk/src/constants.ts @@ -4,3 +4,22 @@ export const TRANSPORT_TYPE_DATACHANNEL = 'datachannel' as const export const SORA_ROLE_SENDRECV = 'sendrecv' as const export const SORA_ROLE_SENDONLY = 'sendonly' as const export const SORA_ROLE_RECVONLY = 'recvonly' as const + +export const SIGNALING_MESSAGE_TYPE_CONNECT = 'connect' as const +export const SIGNALING_MESSAGE_TYPE_REDIRECT = 'redirect' as const +export const SIGNALING_MESSAGE_TYPE_OFFER = 'offer' as const +export const SIGNALING_MESSAGE_TYPE_ANSWER = 'answer' as const +export const SIGNALING_MESSAGE_TYPE_CANDIDATE = 'candidate' as const +export const SIGNALING_MESSAGE_TYPE_SWITCHED = 'switched' as const +export const SIGNALING_MESSAGE_TYPE_RE_OFFER = 're-offer' as const +export const SIGNALING_MESSAGE_TYPE_RE_ANSWER = 're-answer' as const +export const SIGNALING_MESSAGE_TYPE_CLOSE = 'close' as const +export const SIGNALING_MESSAGE_TYPE_DISCONNECT = 'disconnect' as const + +export const SIGNALING_MESSAGE_TYPE_NOTIFY = 'notify' as const +export const SIGNALING_MESSAGE_TYPE_PUSH = 'push' as const +export const SIGNALING_MESSAGE_TYPE_PING = 'ping' as const +export const SIGNALING_MESSAGE_TYPE_PONG = 'pong' as const + +// @deprecated この定数は将来的に削除される予定です +export const SIGNALING_MESSAGE_TYPE_UPDATE = 'update' as const diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 074c35c8..e66a4229 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,4 +1,14 @@ import type { + SIGNALING_MESSAGE_TYPE_CLOSE, + SIGNALING_MESSAGE_TYPE_CONNECT, + SIGNALING_MESSAGE_TYPE_NOTIFY, + SIGNALING_MESSAGE_TYPE_OFFER, + SIGNALING_MESSAGE_TYPE_PING, + SIGNALING_MESSAGE_TYPE_PUSH, + SIGNALING_MESSAGE_TYPE_REDIRECT, + SIGNALING_MESSAGE_TYPE_RE_OFFER, + SIGNALING_MESSAGE_TYPE_SWITCHED, + SIGNALING_MESSAGE_TYPE_UPDATE, SORA_ROLE_RECVONLY, SORA_ROLE_SENDONLY, SORA_ROLE_SENDRECV, @@ -65,7 +75,7 @@ export type SignalingConnectDataChannel = { } export type SignalingConnectMessage = { - type: 'connect' + type: typeof SIGNALING_MESSAGE_TYPE_CONNECT role: Role channel_id: string client_id?: string @@ -112,7 +122,7 @@ export type SignalingOfferMessageDataChannel = { } export type SignalingOfferMessage = { - type: 'offer' + type: typeof SIGNALING_MESSAGE_TYPE_OFFER sdp: string multistream: boolean @@ -141,22 +151,22 @@ export type SignalingOfferMessage = { // @deprecated この型は非推奨です。将来のバージョンで削除される可能性があります。 export type SignalingUpdateMessage = { - type: 'update' + type: typeof SIGNALING_MESSAGE_TYPE_UPDATE sdp: string } export type SignalingReOfferMessage = { - type: 're-offer' + type: typeof SIGNALING_MESSAGE_TYPE_RE_OFFER sdp: string } export type SignalingPingMessage = { - type: 'ping' + type: typeof SIGNALING_MESSAGE_TYPE_PING stats: boolean } export type SignalingPushMessage = { - type: 'push' + type: typeof SIGNALING_MESSAGE_TYPE_PUSH data: Record } @@ -165,18 +175,18 @@ export type SignalingReqStatsMessage = { } export type SignalingSwitchedMessage = { - type: 'switched' + type: typeof SIGNALING_MESSAGE_TYPE_SWITCHED ignore_disconnect_websocket: boolean } export type SignalingRedirectMessage = { - type: 'redirect' + type: typeof SIGNALING_MESSAGE_TYPE_REDIRECT location: string } // DataChannel シグナリングでのみ利用される export type SignalingCloseMessage = { - type: 'close' + type: typeof SIGNALING_MESSAGE_TYPE_CLOSE code: number reason: string } @@ -199,7 +209,7 @@ export type SignalingNotifyMetadata = { } export type SignalingNotifyConnectionCreated = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'connection.created' role: Role client_id?: string @@ -220,7 +230,7 @@ export type SignalingNotifyConnectionCreated = { } export type SignalingNotifyConnectionUpdated = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'connection.updated' role: Role client_id?: string @@ -236,7 +246,7 @@ export type SignalingNotifyConnectionUpdated = { } export type SignalingNotifyConnectionDestroyed = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'connection.destroyed' role: Role client_id?: string @@ -255,7 +265,7 @@ export type SignalingNotifyConnectionDestroyed = { } export type SignalingNotifySpotlightChanged = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'spotlight.changed' client_id: string | null connection_id: string | null @@ -266,7 +276,7 @@ export type SignalingNotifySpotlightChanged = { } export type SignalingNotifySpotlightFocused = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'spotlight.focused' client_id: string | null connection_id: string @@ -276,7 +286,7 @@ export type SignalingNotifySpotlightFocused = { } export type SignalingNotifySpotlightUnfocused = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'spotlight.unfocused' client_id: string | null connection_id: string @@ -286,7 +296,7 @@ export type SignalingNotifySpotlightUnfocused = { } export type SignalingNotifyNetworkStatus = { - type: 'notify' + type: typeof SIGNALING_MESSAGE_TYPE_NOTIFY event_type: 'network.status' unstable_level: 0 | 1 | 2 | 3 }