generated from jonasfroeller/gh-pages-md-auto-publish
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a692556
commit 294fa13
Showing
6 changed files
with
322 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Livewire; | ||
|
||
use Livewire\Component; | ||
use Illuminate\Support\Facades\Http; | ||
use WebSocket\Client as WebSocketClient; # 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) | ||
|
||
class ChatClient extends Component | ||
{ | ||
public $message; | ||
public $monitorId = 'w32tgse'; | ||
public $token; | ||
public $websocket; | ||
public $output = ''; | ||
|
||
public function mount() | ||
{ | ||
$this->login(); | ||
} | ||
|
||
public function login() | ||
{ | ||
$response = Http::post('http://127.0.0.1:6969/login', [ | ||
'email' => 'j.froe@gmx.at', | ||
'password' => 'password', | ||
'onitor_id' => $this->monitorId, | ||
]); | ||
|
||
$this->token = $response->body(); | ||
$this->connectToWebSocket(); | ||
} | ||
|
||
public function connectToWebSocket() | ||
{ | ||
$wsUri = "ws://localhost:6969/chat/{$this->monitorId}?auth={$this->token}"; | ||
$this->websocket = new WebSocketClient($wsUri); | ||
|
||
$this->websocket->onOpen = function () { | ||
$this->output.= "<span class='sticky top-0 left-0 px-2 font-black bg-gray-300 text-lime-900'>CONNECTED</span>"; | ||
$this->sendMessage('ping'); | ||
/* pingInterval = setInterval(function () { // TODO: implement ping | ||
$this->sendMessage('ping'); | ||
}, 5000); */ | ||
}; | ||
|
||
$this->websocket->onClose = function () { | ||
$this->output.= "<span class='sticky top-0 left-0 px-2 font-black text-red-800 bg-gray-300'>CLOSED</span>"; | ||
/* clearInterval($this->pingInterval); */ // TODO: implement ping | ||
}; | ||
|
||
$this->websocket->onMessage = function ($e) { | ||
$this->output.= "<span class='text-right'>{$e->getContent()}</span>"; | ||
}; | ||
|
||
$this->websocket->onError = function () { | ||
$this->output.= "<span class='sticky top-0 left-0 px-2 font-black text-red-800 bg-gray-300'>ERROR</span>"; | ||
}; | ||
} | ||
|
||
public function sendMessage($message) | ||
{ | ||
if ($this->websocket && $message) { | ||
$this->output.= "<span class='text-left'>{$message}</span>"; | ||
$this->websocket->send($message); | ||
$this->message = ''; | ||
} | ||
} | ||
|
||
public function render() | ||
{ | ||
return view('livewire.chat-client'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
laravel-chat/resources/views/livewire/chat/client.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<div class="p-8 prose"> | ||
<h1>Laravel Chat Client Demo</h1> | ||
|
||
<div class="relative flex flex-col overflow-auto h-96" id="output"> | ||
{!! $output !!} | ||
</div> | ||
|
||
<form wire:submit.prevent="sendMessage"> | ||
<input type="text" wire:model="message" id="message" placeholder="Message"> | ||
<button class="h-full px-4 py-2 text-gray-200 bg-gray-800" type="submit"> | ||
Send | ||
</button> | ||
</form> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters