是否可以(异步)读取UDP数据包并将其使用ReactPHP和Async MySQL写入数据库中?

时间:2018-11-17 16:41:09

标签: php oop asynchronous udp reactphp

是否可以使用ReactPHP和用于ReactPHP的异步MySQL数据库客户端(异步)读取udp数据包并将其写入数据库中? 我尝试了一些操作,但是我的代码返回了以下错误:

PHP Notice:  Undefined variable: logNumber in /var/www/html/reactphp/test_react.php on line 23
PHP Notice:  Undefined variable: server in /var/www/html/reactphp/test_react.php on line 30

我想接受许多设备(异步)到给定端口的连接并进行ip并将它们发送给我的数据写入数据库。

是否有比ReaktPHP更好的方法?

我尝试这样的操作:

<?php
use React\MySQL\ConnectionInterface;
use React\MySQL\Factory;
use React\MySQL\QueryResult;
require_once __DIR__.'/vendor/autoload.php';
require_once 'socket_function.php';
$loop = React\EventLoop\Factory::create();
$factory = new React\Datagram\Factory($loop);
$factory->createServer('192.168.1.101:12345')->then(function (React\Datagram\Socket $server) {
    $server->on('message', function($buf, $address, $server) {
        echo 'client ' . $address . ': ' . $buf . PHP_EOL;
        $devId = ascii_decimal($buf, 0, 1);
        $logNumber = ascii_decimal($buf, 12, 15);
        $dbloop = React\EventLoop\Factory::create();
        $dbfactory = new Factory($dbloop);
        $uri = 'user:pass@localhost/dbname';
        $query = "SELECT * FROM devicesregistrator WHERE devid ='".$devId."' ORDER BY id ASC LIMIT 1";
        $dbfactory->createConnection($uri)->then(function (ConnectionInterface $connection) use ($query) {
        $connection->query($query)->then(function (QueryResult $result) {
            if (isset($result->resultRows[0]) and $result->resultRows[0] !== NULL ) {
                $device = $result->resultRows[0]['devid'];
                $logscheck = $result->resultRows[0]['logscheck'];
                if(isset($device) and $device !== NULL and $logNumber > $logscheck+1){
                    $myLogNumber = (int)$logscheck+1;
                    $command = decimal_ascii(83, $device, 0, $myLogNumber);
                }
                else{
                    $command = decimal_ascii(84, $device, 0, time());
                }
                $server->send($command);
            }
        }, function (Exception $error) {
            echo 'Error: ' . $error->getMessage() . PHP_EOL;
        });
        $connection->quit();
    }, 'printf');
    $dbloop->run();
   });
});
$loop->run();
?>  

0 个答案:

没有答案