|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace leocata\m1Bot\Workers; |
|
4
|
|
|
|
|
5
|
|
|
use leocata\M1\HttpClientAuthorization; |
|
6
|
|
|
use leocata\m1Bot\Events; |
|
7
|
|
|
use Ratchet\Client\Connector; |
|
8
|
|
|
use Ratchet\Client\WebSocket; |
|
9
|
|
|
use Ratchet\RFC6455\Messaging\MessageInterface; |
|
10
|
|
|
use React\EventLoop\Factory; |
|
11
|
|
|
|
|
12
|
|
|
class WebSocketWorker extends Events |
|
13
|
|
|
{ |
|
14
|
|
|
private $subProtocol = 'json.api.smile-soft.com'; |
|
15
|
|
|
private $wssHost = 'wss://m1online.net'; |
|
16
|
|
|
private $host = 'm1online.net'; |
|
17
|
|
|
private $auth; |
|
18
|
|
|
private $events; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(HttpClientAuthorization $auth, Events $events) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->events = $events; |
|
23
|
|
|
$this->auth = $auth; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function execute() |
|
27
|
|
|
{ |
|
28
|
|
|
$loop = Factory::create(); |
|
29
|
|
|
$connector = new Connector($loop); |
|
30
|
|
|
|
|
31
|
|
|
$connector($this->wssHost, [$this->subProtocol], ['Host' => $this->host] + $this->auth->getBasicAuth())->then( |
|
32
|
|
|
|
|
33
|
|
|
function (WebSocket $stream) { |
|
34
|
|
|
$stream->on('message', function (MessageInterface $msg) { |
|
35
|
|
|
$this->message($msg); |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
$stream->on('close', function ($code = null, $reason = null) { |
|
39
|
|
|
$this->close($code, $reason); |
|
40
|
|
|
}); |
|
41
|
|
|
}, function (\Exception $e) use ($loop) { |
|
42
|
|
|
echo "Could not connect: {$e->getMessage()}\n"; |
|
43
|
|
|
$loop->stop(); |
|
44
|
|
|
} |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
$loop->run(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getHost(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->host; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $host |
|
60
|
|
|
*/ |
|
61
|
|
|
public function setHost(string $host) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->host = $host; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getWssHost(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->wssHost; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $wssHost |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setWssHost(string $wssHost) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->wssHost = $wssHost; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getSubProtocol(): string |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->subProtocol; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $subProtocol |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setSubProtocol(string $subProtocol) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->subProtocol = $subProtocol; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|