Skip to content
This repository has been archived by the owner on Jun 20, 2021. It is now read-only.

Modify socket reading #2

Merged
merged 3 commits into from
Apr 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/YeePHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ protected function connect(): bool
$sock = fsockopen($this->lightIP, $this->lightPort, $errCode, $errStr, 30);
if (!$sock) return false;

// stream_set_blocking($sock, false);
stream_set_blocking($sock, false);
$this->socket = $sock;

return true;
Expand Down Expand Up @@ -451,10 +451,11 @@ protected function makeRequest(array $job): ?array
fwrite($this->socket, $requestStr);
fflush($this->socket);

// usleep(100 * 1000); // 0.7s -> wait for the light response
usleep(100 * 1000); // 0.7s -> wait for the light response


$res = fgets($this->socket);
$res = '';
while ($out = stream_get_line($this->socket, 4096, "\r\n"))
$res = $out;


$result = null;
Expand All @@ -464,9 +465,10 @@ protected function makeRequest(array $job): ?array

if (!array_key_exists('error', $res) && array_key_exists('result', $res))
$result = $res['result'];
} else {
throw new Exception('No response received from device !');
}


return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/LightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LightTest extends TestCase
*
* @var string
*/
private string $ip = '192.168.0.103';
private string $ip = '192.168.1.21';

private YeePHP $light;

Expand Down