Processes and stores an awareness update from a client.
Parameters
$roomstringrequired- Room identifier.
$client_idintrequired- Client identifier.
- mixed>|null $awareness_update Awareness state sent by the client.
Source
private function process_awareness_update( string $room, int $client_id, ?array $awareness_update ): array {
$existing_awareness = $this->storage->get_awareness_state( $room );
$updated_awareness = array();
$current_time = time();
foreach ( $existing_awareness as $entry ) {
// Remove this client's entry (it will be updated below).
if ( $client_id === $entry['client_id'] ) {
continue;
}
// Remove entries that have expired.
if ( $current_time - $entry['updated_at'] >= self::AWARENESS_TIMEOUT ) {
continue;
}
$updated_awareness[] = $entry;
}
// Add this client's awareness state.
if ( null !== $awareness_update ) {
$updated_awareness[] = array(
'client_id' => $client_id,
'state' => $awareness_update,
'updated_at' => $current_time,
'wp_user_id' => get_current_user_id(),
);
}
// This action can fail, but it shouldn't fail the entire request.
$this->storage->set_awareness_state( $room, $updated_awareness );
// Convert to client_id => state map for response.
$response = array();
foreach ( $updated_awareness as $entry ) {
$response[ $entry['client_id'] ] = $entry['state'];
}
return $response;
}
Changelog
| Version | Description |
|---|---|
| 7.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.