-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathFactory.php
65 lines (50 loc) · 1.61 KB
/
Factory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace React\Stomp;
use React\EventLoop\LoopInterface;
use React\Stomp\Exception\ConnectionException;
use React\Stomp\Io\InputStream;
use React\Stomp\Io\OutputStream;
use React\Stomp\Protocol\Parser;
use React\Socket\Connection;
class Factory
{
private $defaultOptions = array(
'host' => '127.0.0.1',
'port' => 61613,
'vhost' => '/',
'login' => 'guest',
'passcode' => 'guest',
);
private $loop;
public function __construct(LoopInterface $loop)
{
$this->loop = $loop;
}
public function createClient(array $options = array())
{
$options = array_merge($this->defaultOptions, $options);
$conn = $this->createConnection($options);
$parser = new Parser();
$input = new InputStream($parser);
$conn->pipe($input);
$output = new OutputStream($this->loop);
$output->pipe($conn);
$conn->on('error', function ($e) use ($input) {
$input->emit('error', array($e));
});
$conn->on('close', function () use ($input) {
$input->emit('close');
});
return new Client($this->loop, $input, $output, $options);
}
public function createConnection($options)
{
$address = 'tcp://'.$options['host'].':'.$options['port'];
if (false === $fd = @stream_socket_client($address, $errno, $errstr)) {
$message = "Could not bind to $address: $errstr";
throw new ConnectionException($message, $errno);
}
$conn = new Connection($fd, $this->loop);
return $conn;
}
}