Skip to content

Commit 5c06c8b

Browse files
committed
Added support for bidirectional audio websockets
1 parent fb967af commit 5c06c8b

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

src/OpenTok/OpenTok.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,7 @@ public function signal($sessionId, $payload, $connectionId = null)
13291329
* <li><code>'uri'</code> (string) &mdash; A publically reachable WebSocket URI controlled by the customer for the destination of the connect call. (f.e. wss://service.com/wsendpoint)</li>
13301330
* <li><code>'streams'</code> (array) &mdash; (Optional) The stream IDs of the participants' whose audio is going to be connected. If not provided, all streams in session will be selected.</li>
13311331
* <li><code>'headers'</code> (array) &mdash; (Optional) An object of key/val pairs with additional properties to send to your Websocket server, with a maximum length of 512 bytes.</li>
1332+
* <li><code>'bidirectional'</code> (boolean) &mdash; (Optional) Whether the WebSocket connection should be bidirectional. Default is false if not provided.</li>
13321333
* </ul>
13331334
*
13341335
* @return array $response Response from the API, structured as follows:

src/OpenTok/Util/Validators.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,9 @@ public static function validateWebsocketOptions(array $websocketOptions)
435435
if (!array_key_exists('uri', $websocketOptions)) {
436436
throw new InvalidArgumentException('Websocket configuration must have a uri');
437437
}
438+
if (array_key_exists('bidirectional', $websocketOptions) && !is_bool($websocketOptions['bidirectional'])) {
439+
throw new InvalidArgumentException('Websocket configuration bidirectional option must be a boolean');
440+
}
438441
}
439442

440443
public static function validateAutoArchiveResolution($archiveResolution)

tests/OpenTokTest/OpenTokTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3093,4 +3093,78 @@ public function testCanStopCaptions(): void
30933093
$result = $this->opentok->stopCaptions('7c0680fc-6274-4de5-a66f-d0648e8d3ac2');
30943094
$this->assertTrue($result);
30953095
}
3096+
3097+
public function testCanConnectAudioStreamWithBidirectionalWebsocketTrue()
3098+
{
3099+
$this->setupOTWithMocks([[
3100+
'code' => 200,
3101+
'headers' => [
3102+
'Content-Type' => 'application/json'
3103+
],
3104+
'path' => '/v2/project/APIKEY/connect'
3105+
]]);
3106+
3107+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
3108+
$token = '063e72a4-64b4-43c8-9da5-eca071daab89';
3109+
$websocketConfig = [
3110+
'uri' => 'ws://service.com/wsendpoint',
3111+
'streams' => [
3112+
'we9r885',
3113+
'9238fujs'
3114+
],
3115+
'headers' => [
3116+
'key1' => 'value'
3117+
],
3118+
'bidirectional' => true
3119+
];
3120+
3121+
$response = $this->opentok->connectAudio($sessionId, $token, $websocketConfig);
3122+
$this->assertEquals('063e72a4-64b4-43c8-9da5-eca071daab89', $response['id']);
3123+
$this->assertEquals('7aebb3a4-3d86-4962-b317-afb73e05439d', $response['connectionId']);
3124+
}
3125+
3126+
public function testCanConnectAudioStreamWithBidirectionalWebsocketFalse()
3127+
{
3128+
$this->setupOTWithMocks([[
3129+
'code' => 200,
3130+
'headers' => [
3131+
'Content-Type' => 'application/json'
3132+
],
3133+
'path' => '/v2/project/APIKEY/connect'
3134+
]]);
3135+
3136+
$sessionId = '1_MX4xMjM0NTY3OH4-VGh1IEZlYiAyNyAwNDozODozMSBQU1QgMjAxNH4wLjI0NDgyMjI';
3137+
$token = '063e72a4-64b4-43c8-9da5-eca071daab89';
3138+
$websocketConfig = [
3139+
'uri' => 'ws://service.com/wsendpoint',
3140+
'streams' => [
3141+
'we9r885',
3142+
'9238fujs'
3143+
],
3144+
'headers' => [
3145+
'key1' => 'value'
3146+
],
3147+
'bidirectional' => false
3148+
];
3149+
3150+
$response = $this->opentok->connectAudio($sessionId, $token, $websocketConfig);
3151+
$this->assertEquals('063e72a4-64b4-43c8-9da5-eca071daab89', $response['id']);
3152+
$this->assertEquals('7aebb3a4-3d86-4962-b317-afb73e05439d', $response['connectionId']);
3153+
}
3154+
3155+
public function testConnectAudioStreamWithInvalidBidirectionalThrows()
3156+
{
3157+
$this->expectException(InvalidArgumentException::class);
3158+
$this->expectErrorMessage('Websocket configuration bidirectional option must be a boolean');
3159+
$this->setupOTWithMocks([[
3160+
'code' => 200
3161+
]]);
3162+
3163+
$badPayload = [
3164+
'uri' => 'ws://service.com/wsendpoint',
3165+
'bidirectional' => 'notaboolean'
3166+
];
3167+
3168+
$this->opentok->connectAudio('9999', 'wrwetg', $badPayload);
3169+
}
30963170
}

0 commit comments

Comments
 (0)