Advertisement
binemmanuel

PHP Socket.io

Apr 12th, 2021
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app;
  4.  
  5. ini_set('memory_limit', '40m');
  6.  
  7. use app\User;
  8. use Ratchet\ConnectionInterface;
  9. use Ratchet\MessageComponentInterface;
  10.  
  11. class Notification extends User implements MessageComponentInterface
  12. {
  13.     protected $clients;
  14.     private $subscriptions;
  15.     private $users;
  16.  
  17.     public function __construct()
  18.     {
  19.         $this->clients = new \SplObjectStorage;
  20.     }
  21.  
  22.     public function onOpen(ConnectionInterface $conn)
  23.     {
  24.         $this->clients->attach($conn);
  25.  
  26.         echo 'Connecting...';
  27.  
  28.         $queryString = $conn->httpRequest->getUri()->getQuery();
  29.         $queryArray = explode('=', $queryString);
  30.  
  31.         $this->users[$queryArray[1]] = $conn;
  32.  
  33.         echo "New user connected! ({$queryArray[1]})";
  34.     }
  35.  
  36.     public function onMessage(ConnectionInterface $from, $message)
  37.     {
  38.         $message = json_decode($message, true);
  39.  
  40.         echo "\n";
  41.         echo "\n";
  42.         echo "\n";
  43.         echo print_r($message, true);
  44.  
  45.  
  46.  
  47.         $numRecv = count($this->users) - 1;
  48.         // echo sprintf("Number of connected users: %d \n", $numRecv);
  49.  
  50.         $receiver = (new LoginToken)->getUserToken($message['receiver-id']);
  51.  
  52.  
  53.         /* echo "\n";
  54.         echo "\n";
  55.         echo "Is receiver online: ";
  56.         echo "Receiver: $receiver \n\n\n";
  57.         print((int) array_key_exists($receiver, $this->users)); */
  58.  
  59.         // Create a new transaction
  60.         $order = new Order;
  61.  
  62.         $customer = (new User)->getUserByToken($message['login-token'])['id'];
  63.  
  64.         $order->setOrderOption($message['order-option']);
  65.         $order->setQuantity($message['quantity']);
  66.         $order->setPrice($message['price']);
  67.         $order->setDeliveryAddress($message['delivery-address']);
  68.         $order->setPaymentOption($message['payment-option']);
  69.         // $order->setCustomer($customer);
  70.  
  71.         // Save order to DB
  72.         $order->save();
  73.  
  74.         if (array_key_exists($receiver, $this->users))
  75.             // Send a notication to a logged in Admin
  76.             $this->users[$receiver]->send($message);
  77.     }
  78.  
  79.     public function onClose(ConnectionInterface $conn)
  80.     {
  81.         // The connection is closed, remove it, as we can no longer send it messages
  82.         $this->clients->detach($conn);
  83.  
  84.         echo "Connection {$conn->resourceId} has disconnected\n";
  85.     }
  86.  
  87.     public function onError(ConnectionInterface $conn, \Exception $e)
  88.     {
  89.         echo "An error has occurred: {$e->getMessage()}\n";
  90.  
  91.         $conn->close();
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement