Skip to content

Commit

Permalink
#38: Add broadCastMany method to Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Kushmantsev committed Jul 17, 2019
1 parent 1675cb2 commit 0917cba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Components/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ public function broadCast(string $data): void
}
}

/**
* Broadcasting many messages with delay
*
* @param array $data An array of messages (strings) sent to many clients
* @param int $delay Time in seconds to delay between messages
* @throws \Exception
*/
public function broadCastMany(array $data, int $delay = 0): void
{
foreach ($data as $message) {
foreach ($this->clients as $client) {
if (is_resource($client)) { // check if not yet closed/broken etc
fwrite($client, $this->encode($message));
}
}

if ($delay > 0) {
sleep($delay);
}
}
}

/**
* Encodes data before writing to the client socket stream
*
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/ConnectionContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ public function getUniqueSocketId(): int;
public function getPeerName(): string;

public function broadCast(string $data): void;

public function broadCastMany(array $data, int $delay): void;
}

0 comments on commit 0917cba

Please sign in to comment.