From 0e9c7c1dd3aced9a6fa3523d2f25bb984ec8aac3 Mon Sep 17 00:00:00 2001 From: voluntas Date: Wed, 20 Nov 2024 08:59:38 +0900 Subject: [PATCH] =?UTF-8?q?messaging=20=E5=B0=82=E7=94=A8=E3=81=AE?= =?UTF-8?q?=E9=96=A2=E6=95=B0=E3=82=92=E7=94=A8=E6=84=8F=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.md | 5 ++++ examples/messaging/main.mts | 9 +++---- packages/sdk/src/messaging.ts | 48 +++++++++++++++++++++++++++++++++++ packages/sdk/src/sora.ts | 22 ++++++++++++++++ 4 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 packages/sdk/src/messaging.ts diff --git a/CHANGES.md b/CHANGES.md index 3f237cf8..c0b399bf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,11 @@ ## develop +- [ADD] Sora.messaging() を追加する + - @voluntas +- [ADD] messaging_only 専用のクラスを追加する + - ConnectionMessaging + - @voluntas - [ADD] シグナリング項目に `forwarding_filters` を追加する - @voluntas - [ADD] DataChannel メッセージング機能のヘッダー項目を追加する diff --git a/examples/messaging/main.mts b/examples/messaging/main.mts index 8408ef63..1785c6b7 100644 --- a/examples/messaging/main.mts +++ b/examples/messaging/main.mts @@ -1,6 +1,6 @@ import Sora, { type SoraConnection, - type ConnectionPublisher, + type ConnectionMessaging, type SignalingNotifyMessage, type DataChannelMessageEvent, type DataChannelEvent, @@ -84,7 +84,7 @@ class SoraClient { private options: object private sora: SoraConnection - private connection: ConnectionPublisher + private connection: ConnectionMessaging constructor( signalingUrl: string, channelIdPrefix: string, @@ -107,7 +107,7 @@ class SoraClient { ], } - this.connection = this.sora.sendonly(this.channelId, this.metadata, this.options) + this.connection = this.sora.messaging(this.channelId, this.metadata, this.options) this.connection.on('notify', this.onnotify.bind(this)) this.connection.on('datachannel', this.ondatachannel.bind(this)) @@ -131,8 +131,7 @@ class SoraClient { header: header ? [{ type: 'sender_connection_id' }] : undefined, }, ] - // sendonly の場合は空の MediaStream を渡す - await this.connection.connect(new MediaStream()) + await this.connection.connect() // disconnect ボタンを有効にする const disconnectButton = document.querySelector('#disconnect') diff --git a/packages/sdk/src/messaging.ts b/packages/sdk/src/messaging.ts new file mode 100644 index 00000000..3eb246f0 --- /dev/null +++ b/packages/sdk/src/messaging.ts @@ -0,0 +1,48 @@ +import ConnectionBase from './base' + +/** + * messaging_only 専用のクラス + * 利用する場合は Sora 側での設定が必要 + * Role は "sendonly" に固定される + */ +export default class ConnectionMessaging extends ConnectionBase { + /** + * Sora へ接続するメソッド、legacyStream は利用できない + * + * @example + * ```typescript + * const messaging = connection.messaging("sora"); + * await messaging.connect(); + * ``` + * + * @public + */ + async connect(): Promise { + // options が + await Promise.race([ + this.multiStream().finally(() => { + this.clearConnectionTimeout() + this.clearMonitorSignalingWebSocketEvent() + }), + this.setConnectionTimeout(), + this.monitorSignalingWebSocketEvent(), + ]) + this.monitorWebSocketEvent() + this.monitorPeerConnectionState() + } + + /** + * マルチストリームで Sora へ接続するメソッド + */ + private async multiStream(): Promise { + await this.disconnect() + const ws = await this.getSignalingWebSocket(this.signalingUrlCandidates) + const signalingMessage = await this.signaling(ws) + await this.connectPeerConnection(signalingMessage) + await this.setRemoteDescription(signalingMessage) + await this.createAnswer(signalingMessage) + this.sendAnswer() + await this.onIceCandidate() + await this.waitChangeConnectionStateConnected() + } +} diff --git a/packages/sdk/src/sora.ts b/packages/sdk/src/sora.ts index 93672c78..b0ae4ab6 100644 --- a/packages/sdk/src/sora.ts +++ b/packages/sdk/src/sora.ts @@ -1,5 +1,6 @@ import type ConnectionBase from './base' import { applyMediaStreamConstraints } from './helpers' +import ConnectionMessaging from './messaging' import ConnectionPublisher from './publisher' import ConnectionSubscriber from './subscriber' import type { @@ -158,6 +159,26 @@ class SoraConnection { this.debug, ) } + + messaging( + channelId: string, + metadata: JSONType = null, + options: ConnectionOptions = { audio: false, video: false }, + ): ConnectionMessaging { + options.audio = false + options.video = false + options.multistream = true + return new ConnectionMessaging( + this.signalingUrlCandidates, + // messaging は role sendonly として扱う + 'sendonly', + channelId, + metadata, + options, + this.debug, + ) + } + /** * シグナリングに使用する URL の候補 * @@ -215,6 +236,7 @@ export type { ConnectionOptions, ConnectionPublisher, ConnectionSubscriber, + ConnectionMessaging, DataChannelConfiguration, DataChannelDirection, DataChannelEvent,