mirror of
https://github.com/bitinflow/expose.git
synced 2026-03-13 21:45:55 +00:00
wip
This commit is contained in:
@@ -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())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user