0

I need to read data from a serial port and send it to a websocket. I loop into a while and when some data arrives I throw it out.

Given the code below, which is a very little modification from Pawl's loop example, the send() is never executed and I cannot figure out why. I tried the sending code in a standalone php and it works, but when I add it into the while loop it looks like it's never executed. I see the INVIO debug but then it goes back to LOOP, and no message is broadcasted.

I tried even the easier example without React's loop but behaves exactly the same, send() is apparently never reached.

        $loop = \React\EventLoop\Factory::create();
        $reactConnector = new \React\Socket\Connector($loop, [
          'dns' => '8.8.8.8',
          'timeout' => 10
        ]);
        $connector = new \Ratchet\Client\Connector($loop, $reactConnector);

        $loop->addPeriodicTimer(8, function () use($connector){
            echo "LOOP\n";
            do {
                sleep(1);
                $msg = $this->getSerial()->read();
//              $msg = $this->getSerial()->readPort();
                echo "LETTO <$msg>\n";
           } while (strlen($msg) < 50);
                echo "INVIO $msg\n";
                $connector('ws://127.0.0.1:9988')
                  ->then(function(Ratchet\Client\WebSocket $conn) {
                      $conn->on('close', function($code = null, $reason = null) {
                      echo "Connection closed ({$code} - {$reason})\n";
                  });

                  $conn->send('Hello World!');
                  $conn->close();
                }, function(\Exception $e) {
                  echo "Could not connect: {$e->getMessage()}\n";
                });
        });
        $loop->run();
Maxxer
  • 1,038
  • 1
  • 18
  • 37

1 Answers1

0

Hey ReactPHP core team member here. So there are a few things I notice from your example that should make debugging this easier.

  • Our promises have two major methods. a) then, b) done. then can be chained and used to executed a series of operations that return a promise. done cannot and is considered to be the end of a chain, when an error is thrown but not handled in the chain done will retrow it. There is a change something errors in your code but the exception|throwable is swallowed.
  • Running a while loop inside an event loop is advised against because it will block the loop, and the other operations running inside it. (So does using sleep, you can use a timer to wait another second.)
  • You seem to be opening a connection per message, wouldn't it be more efficient to open a single connection and push every new message to the websocket server over that connection?
WyriHaximus
  • 1,000
  • 6
  • 8
  • indeed I knew the code wasn't really *the best*, but I needed a PoC to work on to test serial and websocket. I will see in the next days if I can improve the code – Maxxer Mar 19 '19 at 17:40
  • 1
    To be honest I never know the level of another developer when helping so I try to give a few pointers when I see orange/red flags that could/will become burden some down the road. And I totally get writing up a PoC (mine aren't much better at times) and not caring about style and best practices. But try the then/done thing out it should reveal errors, alternatively do a then like this: ->then(function () {/** your current code */ }, function ($e) {echo (string)$e;}); – WyriHaximus Mar 19 '19 at 18:08