Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app;
- ini_set('memory_limit', '40m');
- use app\User;
- use Ratchet\ConnectionInterface;
- use Ratchet\MessageComponentInterface;
- class Notification extends User implements MessageComponentInterface
- {
- protected $clients;
- private $subscriptions;
- private $users;
- public function __construct()
- {
- $this->clients = new \SplObjectStorage;
- }
- public function onOpen(ConnectionInterface $conn)
- {
- $this->clients->attach($conn);
- echo 'Connecting...';
- $queryString = $conn->httpRequest->getUri()->getQuery();
- $queryArray = explode('=', $queryString);
- $this->users[$queryArray[1]] = $conn;
- echo "New user connected! ({$queryArray[1]})";
- }
- public function onMessage(ConnectionInterface $from, $message)
- {
- $message = json_decode($message, true);
- echo "\n";
- echo "\n";
- echo "\n";
- echo print_r($message, true);
- $numRecv = count($this->users) - 1;
- // echo sprintf("Number of connected users: %d \n", $numRecv);
- $receiver = (new LoginToken)->getUserToken($message['receiver-id']);
- /* echo "\n";
- echo "\n";
- echo "Is receiver online: ";
- echo "Receiver: $receiver \n\n\n";
- print((int) array_key_exists($receiver, $this->users)); */
- // Create a new transaction
- $order = new Order;
- $customer = (new User)->getUserByToken($message['login-token'])['id'];
- $order->setOrderOption($message['order-option']);
- $order->setQuantity($message['quantity']);
- $order->setPrice($message['price']);
- $order->setDeliveryAddress($message['delivery-address']);
- $order->setPaymentOption($message['payment-option']);
- // $order->setCustomer($customer);
- // Save order to DB
- $order->save();
- if (array_key_exists($receiver, $this->users))
- // Send a notication to a logged in Admin
- $this->users[$receiver]->send($message);
- }
- public function onClose(ConnectionInterface $conn)
- {
- // The connection is closed, remove it, as we can no longer send it messages
- $this->clients->detach($conn);
- echo "Connection {$conn->resourceId} has disconnected\n";
- }
- public function onError(ConnectionInterface $conn, \Exception $e)
- {
- echo "An error has occurred: {$e->getMessage()}\n";
- $conn->close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement