Skip to content

Commit

Permalink
laravel-chat: fixes `Property type not supported in Livewire for prop…
Browse files Browse the repository at this point in the history
…erty: [{}]`, can now connect to chats
  • Loading branch information
jonasfroeller committed Jun 13, 2024
1 parent afc4f61 commit 7c52cec
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions laravel-chat/app/Livewire/ChatClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# waiting for v3 release (Declaration of WebSocket\Client::setLogger(Psr\Log\LoggerInterface $logger): WebSocket\Client must be compatible with Psr\Log\LoggerAwareInterface::setLogger(Psr\Log\LoggerInterface $logger): void)

// TODO: implement joining, only if user clicks chat in ui, to reduce server load and loading time
// TODO: implement joining, only if user clicks chat in ui, to reduce server load and loading time (far future)

use Livewire\Component;
use Illuminate\Support\Facades\Http;
Expand Down Expand Up @@ -104,7 +104,7 @@ class ChatClient extends Component
public String $password = 'password';
public String | null $token = null;

public ?ChatConnectionsDTO $chats = null;
private ?ChatConnectionsDTO $chats = null;

public function mount()
{
Expand Down Expand Up @@ -161,9 +161,7 @@ public function login()
$chats = $json['chats'];

$chatMap = self::parseChats($chats);

$this->chats->setChats($chatMap);
dump($this->chats);

$this->connectToWebSockets();
} else {
Expand All @@ -176,13 +174,15 @@ public function login()

public function connectToWebSockets()
{
foreach ($this->chats as $chat) {
$modifiedChats = [];
foreach ($this->chats->getChats() as $chat) {
$monitorId = rawurlencode($chat['info']['detail']->monitor_hash);
$wsUri = self::getChatUrl($monitorId, $this->token);

$this->chats[$monitorId]['websocket'] = new WebSocketClient($wsUri);
$modifiedChats[$monitorId] = $chat;
$modifiedChats[$monitorId]['websocket'] = new WebSocketClient($wsUri);

$this->chats[$monitorId]['websocket'] // TODO: implement ping - ping every 2 or 3 seconds
$modifiedChats[$monitorId]['websocket'] // TODO: implement ping - ping every 2 or 3 seconds
->addMiddleware(new WebSocketMiddleware\CloseHandler())
->onText(function (WebSocketClient $client, WebSocketConnection $connection, WebSocketMessage $message) {
$monitorId = self::getMonitorOfClient($client);
Expand All @@ -208,6 +208,10 @@ public function connectToWebSockets()
})
->start();
}

$this->chats->setChats($modifiedChats);

dump($this->chats);
}

public function sendMessage(String $monitorId, String $message)
Expand Down

0 comments on commit 7c52cec

Please sign in to comment.