Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit d19c711

Browse files
authored
Merge pull request repejota#90 from dfeyer/patch-2
TASK: More solid method to send payload to the socket
2 parents 614051f + 5abcbe2 commit d19c711

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

src/Nats/Connection.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,21 @@ public function __construct(ConnectionOptions $options = null)
148148
private function send($payload)
149149
{
150150
$msg = $payload."\r\n";
151-
fwrite($this->streamSocket, $msg, strlen($msg));
151+
$len = strlen($msg);
152+
while (true) {
153+
if (false === ($written = @fwrite($this->streamSocket, $msg))) {
154+
throw new \Exception('Error sending data');
155+
}
156+
if ($written === 0) {
157+
throw new \Exception('Broken pipe or closed connection');
158+
}
159+
$len = $len - $written;
160+
if ($len > 0) {
161+
$msg = substr($msg, 0 - $len);
162+
} else {
163+
break;
164+
}
165+
}
152166
}
153167

154168
/**

0 commit comments

Comments
 (0)