/** * Connect to server * * @return boolean * @throws StompException * @see setVhostname */ public function connect() { if ($this->isConnected()) { return true; } $this->isConnecting = true; $this->connection->connect(); $this->connection->getParser()->legacyMode(true); $this->protocol = new Protocol($this->clientId); $this->host = $this->host ?: $this->connection->getHost(); $connectFrame = $this->protocol->getConnectFrame($this->login, $this->passcode, $this->versions, $this->host); $this->sendFrame($connectFrame, false); if ($frame = $this->connection->readFrame()) { $version = new Version($frame); if ($version->hasVersion(Version::VERSION_1_1)) { $this->connection->getParser()->legacyMode(false); } $this->sessionId = $frame['session']; $this->protocol = $version->getProtocol($this->clientId); $this->isConnecting = false; 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(ConnectionException::class, $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']); } }