/** * Creates a new Connection * * @param string $host * @param integer $port * @param string|null $password * * @return Connection */ public function createConnection($host, $port, $password = null) { $socket = $this->socketFactory->createClient(sprintf('%s:%s', $host, $port)); $conn = new Connection($socket); if (!empty($password)) { $conn->authenticate($password); } return $conn; }
/** * @param string $dsn * @param string $prompt * @param string $promptError * @param string $lineEnding * * @throws TelnetExceptionInterface */ public function connect($dsn, $prompt = null, $promptError = null, $lineEnding = null) { if ($prompt !== null) { $this->setPrompt($prompt); } if ($promptError !== null) { $this->setPromptError($promptError); } if ($lineEnding !== null) { $this->setLineEnding($lineEnding); } try { $socket = $this->socketFactory->createClient($dsn); } catch (Exception $e) { throw new TelnetException(sprintf('unable to create socket connection to [%s]', $dsn), 0, $e); } $this->setSocket($socket); }
#!/usr/bin/env php <?php use Socket\Raw\Factory; error_reporting(-1); require_once __DIR__ . '/../vendor/autoload.php'; $factory = new Factory(); $socket = $factory->createClient('www.google.com:80'); echo 'Client connected to ' . $socket->getPeerName() . PHP_EOL . PHP_EOL; $socket->write("GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n"); var_dump($socket->read(8192)); $socket->close();
function it_creates_a_connection(SocketFactory $socketFactory, Socket $socket) { $socketFactory->createClient('localhost:25575')->willReturn($socket); $conn = $this->createConnection('localhost', '25575'); $conn->shouldHaveType('Minecraft\\Rcon\\Connection'); }