diff --git a/src/Connection/Socket.php b/src/Connection/Socket.php index 8ce49a7..22e9934 100644 --- a/src/Connection/Socket.php +++ b/src/Connection/Socket.php @@ -89,7 +89,7 @@ public function __construct(SocketClient $socket) */ public static function factory(Options $options) { - $socket = new SocketClient($options->getAddress()); + $socket = new SocketClient($options->getAddress(), $options->getContextOptions()); $object = new static($socket); $object->setOptions($options); return $object; diff --git a/src/Options.php b/src/Options.php index dd2fd55..966811a 100644 --- a/src/Options.php +++ b/src/Options.php @@ -128,6 +128,15 @@ class Options 'plain' => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\Plain' ); + + /** + * Options used to create a stream context + * + * @var array + */ + protected $contextOptions = array(); + + /** * Constructor. * @@ -413,4 +422,26 @@ public function setTimeout($timeout) $this->timeout = (int) $timeout; return $this; } + + /** + * Get context options for connection + * + * @return array + */ + public function getContextOptions() + { + return $this->contextOptions; + } + + /** + * Set context options for connection + * + * @param array $contextOptions + * @return \Fabiang\Xmpp\Options + */ + public function setContextOptions($contextOptions) + { + $this->contextOptions = (array) $contextOptions; + return $this; + } } diff --git a/src/Stream/SocketClient.php b/src/Stream/SocketClient.php index afba977..6cf96af 100644 --- a/src/Stream/SocketClient.php +++ b/src/Stream/SocketClient.php @@ -63,20 +63,30 @@ class SocketClient */ protected $address; + + /** + * Options used to create a stream context + * @see http://php.net/manual/en/function.stream-context-create.php + * + * @var array + */ + protected $options; + /** * Constructor takes address as argument. * * @param string $address */ - public function __construct($address) + public function __construct($address, $options = null) { $this->address = $address; + $this->options = $options; } /** * Connect. * - * @param integer $timeout Timeout for connection + * @param integer $timeout Timeout for connection * @param boolean $persistent Persitent connection * @return void */ @@ -91,7 +101,8 @@ public function connect($timeout = 30, $persistent = false) // call stream_socket_client with custom error handler enabled $handler = new ErrorHandler( function ($address, $timeout, $flags) { - return stream_socket_client($address, $errno, $errstr, $timeout, $flags); + $context = (null != $this->options) ? stream_context_create($this->options) : null; + return stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context); }, $this->address, $timeout, @@ -106,9 +117,9 @@ function ($address, $timeout, $flags) { /** * Reconnect and optionally use different address. * - * @param string $address + * @param string $address * @param integer $timeout - * @param bool $persistent + * @param bool $persistent */ public function reconnect($address = null, $timeout = 30, $persistent = false) { @@ -139,7 +150,7 @@ public function close() */ public function setBlocking($flag = true) { - stream_set_blocking($this->resource, (int) $flag); + stream_set_blocking($this->resource, (int)$flag); return $this; } @@ -157,7 +168,7 @@ public function read($length = self::BUFFER_LENGTH) /** * Write to stream. * - * @param string $string String + * @param string $string String * @param integer $length Limit * @return void */ @@ -173,7 +184,7 @@ public function write($string, $length = null) /** * Enable/disable cryptography on stream. * - * @param boolean $enable Flag + * @param boolean $enable Flag * @param integer $cryptoType One of the STREAM_CRYPTO_METHOD_* constants. * @return void * @throws InvalidArgumentException