Skip to content

Commit

Permalink
messaging 専用の関数を用意する
Browse files Browse the repository at this point in the history
  • Loading branch information
voluntas committed Nov 19, 2024
1 parent 043f363 commit 0e9c7c1
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

## develop

- [ADD] Sora.messaging() を追加する
- @voluntas
- [ADD] messaging_only 専用のクラスを追加する
- ConnectionMessaging
- @voluntas
- [ADD] シグナリング項目に `forwarding_filters` を追加する
- @voluntas
- [ADD] DataChannel メッセージング機能のヘッダー項目を追加する
Expand Down
9 changes: 4 additions & 5 deletions examples/messaging/main.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Sora, {
type SoraConnection,
type ConnectionPublisher,
type ConnectionMessaging,
type SignalingNotifyMessage,
type DataChannelMessageEvent,
type DataChannelEvent,
Expand Down Expand Up @@ -84,7 +84,7 @@ class SoraClient {
private options: object

private sora: SoraConnection
private connection: ConnectionPublisher
private connection: ConnectionMessaging
constructor(
signalingUrl: string,
channelIdPrefix: string,
Expand All @@ -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))
Expand All @@ -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<HTMLButtonElement>('#disconnect')
Expand Down
48 changes: 48 additions & 0 deletions packages/sdk/src/messaging.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
// options が
await Promise.race([
this.multiStream().finally(() => {
this.clearConnectionTimeout()
this.clearMonitorSignalingWebSocketEvent()
}),
this.setConnectionTimeout(),
this.monitorSignalingWebSocketEvent(),
])
this.monitorWebSocketEvent()
this.monitorPeerConnectionState()
}

/**
* マルチストリームで Sora へ接続するメソッド
*/
private async multiStream(): Promise<void> {
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()
}
}
22 changes: 22 additions & 0 deletions packages/sdk/src/sora.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 の候補
*
Expand Down Expand Up @@ -215,6 +236,7 @@ export type {
ConnectionOptions,
ConnectionPublisher,
ConnectionSubscriber,
ConnectionMessaging,
DataChannelConfiguration,
DataChannelDirection,
DataChannelEvent,
Expand Down

0 comments on commit 0e9c7c1

Please sign in to comment.