client.php 780B

12345678910111213141516171819202122232425262728
  1. <?php
  2. require_once __DIR__ . '/vendor/autoload.php';
  3. require_once __DIR__ . '/ThermalPrinting.php';
  4. try {
  5. $socket = new Hoa\Websocket\Client(
  6. new Hoa\Socket\Client('ws://103.157.96.20:5544')
  7. );
  8. $socket->setHost('escpos-server');
  9. $socket->on('message', function (\Hoa\Event\Bucket$bucket) {
  10. $data = $bucket->getData();
  11. $response = json_decode($data['message']);
  12. echo '> Received request ', $data['message'], "\n";
  13. if($response) {
  14. $thermalPrinting = new ThermalPrinting($response);
  15. $thermalPrinting->startPrinting(2);
  16. } else {
  17. echo 'No data!';
  18. }
  19. });
  20. $socket->run();
  21. $socket->receive();
  22. } catch (Exception $e) {
  23. echo '> Error: ', $e->getMessage(), "\n";
  24. }