Skip to content

Commit

Permalink
Merge pull request #11 from uniondrug/2.x
Browse files Browse the repository at this point in the history
add ping()
  • Loading branch information
xueron authored Apr 26, 2018
2 parents 62c770b + 96c1749 commit 8f5a626
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Servitization/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ public function getProtocol()
return $this->scheme;
}

/**
* 同步客户端,已经连接过,则尝试ping一下。
*
* @return bool
*/
public function ping()
{
if ($this->client->isConnected() && !$this->async) {
$this->client->send('ping');
$res = $this->receive();
return $res === 'pong';
}

return false;
}

/**
* @param string $data
*
Expand Down Expand Up @@ -91,11 +107,11 @@ public function decodeChunked($str)
$thisChunk = '';

// Loop the data
while (($part = array_shift($parts)) !== NULL) {
while (($part = array_shift($parts)) !== null) {
if ($chunkLen) {
// Add the data to the string
// Don't forget, the data might contain a literal CRLF
$thisChunk .= $part."\r\n";
$thisChunk .= $part . "\r\n";
if (strlen($thisChunk) == $chunkLen) {
// Chunk is complete
$result .= $thisChunk;
Expand All @@ -108,7 +124,7 @@ public function decodeChunked($str)
$thisChunk = '';
} else if (strlen($thisChunk) > $chunkLen) {
// Data is malformed
return FALSE;
return false;
}
} else {
// If we are not in a chunk, get length of the new one
Expand All @@ -118,6 +134,6 @@ public function decodeChunked($str)
}

// Return the decoded data of FALSE if it is incomplete
return ($chunkLen) ? FALSE : $result;
return ($chunkLen) ? false : $result;
}
}
1 change: 1 addition & 0 deletions src/Servitization/Server/TCPServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TCPServer extends TCP
public function doWork(swoole_server $server, $fd, $data, $from_id)
{
$data = trim($data);

if ('ping' === $data) {
$server->send($fd, 'pong');
return 0;
Expand Down

0 comments on commit 8f5a626

Please sign in to comment.