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