This commit is contained in:
Marcel Pociot
2020-04-29 22:05:03 +02:00
parent 6cf206e0a2
commit b515a55325
27 changed files with 215 additions and 253 deletions

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use Exception;
use App\WebSockets\Socket;
use GuzzleHttp\Psr7\Response;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Ratchet\ConnectionInterface;
use function GuzzleHttp\Psr7\str;
@@ -14,50 +15,20 @@ use Psr\Http\Message\RequestInterface;
class PushLogsToDashboardController extends Controller
{
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
public function handle(Request $request, ConnectionInterface $httpConnection)
{
$connection->contentLength = $this->findContentLength($request->getHeaders());
$connection->requestBuffer = (string) $request->getBody();
$this->checkContentLength($connection);
}
public function onMessage(ConnectionInterface $from, $msg)
{
$from->requestBuffer .= $msg;
$this->checkContentLength($from);
}
protected function findContentLength(array $headers): int
{
return Collection::make($headers)->first(function ($values, $header) {
return strtolower($header) === 'content-length';
})[0] ?? 0;
}
protected function checkContentLength(ConnectionInterface $connection)
{
if (strlen($connection->requestBuffer) === $connection->contentLength) {
try {
/*
* This is the post payload from our PHPUnit tests.
* Send it to the connected connections.
*/
foreach (Socket::$connections as $webSocketConnection) {
$webSocketConnection->send($connection->requestBuffer);
}
$connection->send(str(new Response(200)));
} catch (Exception $e) {
$connection->send(str(new Response(500, [], $e->getMessage())));
try {
/*
* This is the post payload from our PHPUnit tests.
* Send it to the connected connections.
*/
foreach (Socket::$connections as $webSocketConnection) {
$webSocketConnection->send($request->getContent());
}
$connection->close();
unset($connection->requestBuffer);
unset($connection->contentLength);
$httpConnection->send(str(new Response(200)));
} catch (Exception $e) {
$httpConnection->send(str(new Response(500, [], $e->getMessage())));
}
}
}