-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php namespace com\openai\realtime; | ||
|
||
use text\json\Json; | ||
use util\URI; | ||
use util\data\Marshalling; | ||
use util\log\Traceable; | ||
use websocket\WebSocket; | ||
|
||
/** @see https://github.com/azure-samples/aoai-realtime-audio-sdk */ | ||
class RealtimeApi implements Traceable { | ||
private $ws, $headers; | ||
private $cat= null; | ||
|
||
/** @param string|util.URI $endpoint */ | ||
public function __construct($endpoint) { | ||
$uri= $endpoint instanceof URI ? $endpoint : new URI($endpoint); | ||
$this->ws= new WebSocket($uri); | ||
$this->headers= ['api-key' => $uri->user()]; | ||
$this->marshalling= new Marshalling(); | ||
} | ||
|
||
/** @param ?util.log.LogCategory $cat */ | ||
public function setTrace($cat) { | ||
$this->cat= $cat; | ||
} | ||
|
||
/** Opens the underlying websocket, optionally passing headers */ | ||
public function connect(array $headers= []): self { | ||
$headers+= $this->headers; | ||
$this->cat && $this->cat->info($this->ws->socket(), $this->ws->path(), $headers); | ||
$this->ws->connect($headers); | ||
return $this; | ||
} | ||
|
||
/** Returns whether the underlying websocket is connected */ | ||
public function connected(): bool { | ||
return $this->ws->connected(); | ||
} | ||
|
||
/** Closes the underlying websocket */ | ||
public function close(): void { | ||
$this->ws->close(); | ||
} | ||
|
||
/** | ||
* Sends a given payload. Doesn't wait for a response | ||
* | ||
* @param var $payload | ||
* @return void | ||
*/ | ||
public function send($payload): void { | ||
$json= Json::of($this->marshalling->marshal($payload)); | ||
$this->cat && $this->cat->debug('>>>', $json); | ||
$this->ws->send($json); | ||
} | ||
|
||
/** | ||
* Receives an answer. Returns NULL if EOF is reached. | ||
* | ||
* @return var | ||
*/ | ||
public function receive() { | ||
$json= $this->ws->receive(); | ||
$this->cat && $this->cat->debug('<<<', $json); | ||
return null === $json ? null : $this->marshalling->unmarshal(Json::read($json)); | ||
} | ||
|
||
/** | ||
* Sends a given payload and returns the response to it. | ||
* | ||
* @param var $payload | ||
* @return var | ||
*/ | ||
public function transmit($payload) { | ||
$this->send($payload); | ||
return $this->receive(); | ||
} | ||
|
||
/** Ensures socket is closed */ | ||
public function __destruct() { | ||
$this->ws && $this->ws->close(); | ||
} | ||
} |