/** * @return Connection */ protected function getConnection() { if ($this->_connection === null) { $this->_connection = new Connection(); $this->_connection->connect($this->_connect_uri, $this->_connect_options); $this->_connect(); } return $this->_connection; }
/** * Connect to server * * @param string $login * @param string $passcode * @return boolean * @throws StompException */ public function connect($login = null, $passcode = null) { if ($login !== null) { $this->login = $login; } if ($passcode !== null) { $this->passcode = $passcode; } $this->connection->connect(); $this->protocol = new Protocol($this->prefetchSize, $this->clientId); $this->sendFrame($this->protocol->getConnectFrame($this->login, $this->passcode), false); if ($frame = $this->connection->readFrame()) { if ($frame->command != 'CONNECTED') { throw new UnexpectedResponseException($frame, 'Expected a CONNECTED Frame!'); } $this->sessionId = $frame->headers['session']; $server = isset($frame->headers['server']) ? trim($frame->headers['server']) : false; if ($server && false !== stristr($server, 'rabbitmq')) { $this->brokerVendor = 'RMQ'; $this->protocol = new RabbitMq($this->protocol); } elseif ($server && false !== stristr($server, 'apache-apollo')) { $this->protocol = new Apollo($this->protocol); } else { $this->protocol = new ActiveMq($this->protocol); } return true; } throw new ConnectionException('Connection not acknowledged'); }
public function testConnectionFailLeadsToException() { $connection = new Connection('tcp://0.0.0.1:15'); try { $connection->connect(); $this->fail('Expected an exception!'); } catch (ConnectionException $ex) { $this->assertContains('Could not connect to a broker', $ex->getMessage()); $this->assertInstanceOf('Stomp\\Exception\\ConnectionException', $ex->getPrevious(), 'There should be a previous exception.'); /** @var ConnectionException $prev */ $prev = $ex->getPrevious(); $hostInfo = $prev->getConnectionInfo(); $this->assertEquals('0.0.0.1', $hostInfo['host']); $this->assertEquals('15', $hostInfo['port']); } }