Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created January 21, 2016 16:15
Show Gist options
  • Save EdwardIII/f7f8d23e8fdd87d1e3f8 to your computer and use it in GitHub Desktop.
Save EdwardIII/f7f8d23e8fdd87d1e3f8 to your computer and use it in GitHub Desktop.
ini_set("default_socket_timeout", "1");
class SocketWorker implements
{
private $socketResource;
private $socketErrorNum;
private $socketErrorStr;
public function __construct($socketServer, $socketPort, $socketTimeOut)
{
$this->socketResource = fsockopen($socketServer, $socketPort, $this->socketErrorNum, $this->socketErrorStr, $socketTimeOut);
return (!empty($this->socketResource)) ? true : false;
}
public function __destruct()
{
$this->socketClose();
}
public function socketRequest($socketRequest)
{
$socketReturnByte = fwrite($this->socketResource, $socketRequest, strlen($socketRequest));
return (!empty($socketReturnByte)) ? $socketReturnByte : false;
}
public function socketResponse()
{
$socketResponse = "";
while ($socketRead = fread($this->socketResource, 4096)) {
$socketResponse .= $socketRead;
}
return $socketResponse;
}
public function socketClose()
{
fclose($this->socketResource);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment