changed pollForMessages() impl
This commit is contained in:
+12
-5
@@ -301,26 +301,33 @@ class Meshcore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Poll for messages, returning after timeout or if a message with <tag> is received. This function can
|
||||||
|
* return zero to many results in an array. Not all messages may match <tag>.
|
||||||
*
|
*
|
||||||
* @param string $tag
|
* @param string $tag
|
||||||
* @param int $timeout_ms
|
* @param int $timeout_ms defaults to 5 seconds
|
||||||
* @return mixed
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function pollForMessage(string $tag, int $timeout_ms = 5000): mixed {
|
public function pollForMessage(string $tag, int $timeout_ms = 5000): array {
|
||||||
$mark = time();
|
$mark = time();
|
||||||
|
$messages = [];
|
||||||
|
|
||||||
do {
|
do {
|
||||||
$msg = self::getNextMessage();
|
$msg = self::getNextMessage();
|
||||||
|
|
||||||
|
if( $msg instanceof BinaryResponse ) {
|
||||||
|
$messages[] = $msg;
|
||||||
|
}
|
||||||
|
|
||||||
if( $msg instanceof BinaryResponse && $msg->tag == $tag ) {
|
if( $msg instanceof BinaryResponse && $msg->tag == $tag ) {
|
||||||
return $msg;
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
usleep(500000);
|
usleep(500000);
|
||||||
}
|
}
|
||||||
while( ((time() - $mark) * 1000) < $timeout_ms );
|
while( ((time() - $mark) * 1000) < $timeout_ms );
|
||||||
|
|
||||||
return false;
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -28,7 +28,6 @@ print_r($mc->login($contact['pub_key'], readline('Please enter the password: '))
|
|||||||
|
|
||||||
$result = $mc->statusRequest($contact['pub_key']);
|
$result = $mc->statusRequest($contact['pub_key']);
|
||||||
print_r($result);
|
print_r($result);
|
||||||
exit;
|
|
||||||
|
|
||||||
$msg = $mc->pollForMessage($result->tag);
|
$msg = $mc->pollForMessage($result->tag);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ $repeaters = ['Simpsonville 3810gvr1', 'Simpsonville im37r1'];
|
|||||||
$contacts = $mc->getContacts();
|
$contacts = $mc->getContacts();
|
||||||
|
|
||||||
echo "Found " . count($contacts) . " contacts\n";
|
echo "Found " . count($contacts) . " contacts\n";
|
||||||
|
|
||||||
/*
|
/*
|
||||||
$idx = 1;
|
$idx = 1;
|
||||||
foreach($contacts as $contact) {
|
foreach($contacts as $contact) {
|
||||||
@@ -46,11 +47,13 @@ foreach($contacts as $contact) {
|
|||||||
|
|
||||||
echo "Tag: {$result->tag}\n";
|
echo "Tag: {$result->tag}\n";
|
||||||
|
|
||||||
$msg = $mc->pollForMessage($result->tag);
|
$msgs = $mc->pollForMessage($result->tag);
|
||||||
|
|
||||||
echo json_encode($msg->lpp) . "\n";
|
foreach($msgs as $msg) {
|
||||||
|
if( $msg->tag == $result->tag) {
|
||||||
sleep(1);
|
echo json_encode($msg->lpp) . "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "Complete\n";
|
echo "Complete\n";
|
||||||
|
|||||||
Reference in New Issue
Block a user