1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\SocketIoBundle\Service; |
4
|
|
|
|
5
|
|
|
use Predis\Connection\ConnectionException; |
6
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
7
|
|
|
use Symfony\Component\Process\Process; |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Worker |
11
|
|
|
* |
12
|
|
|
* @author Virchenko Maksim <[email protected]> |
13
|
|
|
* |
14
|
|
|
* @package SfCod\SocketIoBundle\Service |
15
|
|
|
*/ |
16
|
|
|
class Worker |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EventManager |
20
|
|
|
*/ |
21
|
|
|
private $eventManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var RedisDriver |
25
|
|
|
*/ |
26
|
|
|
private $redisDriver; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Broadcast |
30
|
|
|
*/ |
31
|
|
|
private $broadcast; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $logDir; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Worker constructor. |
40
|
|
|
* |
41
|
|
|
* @param EventManager $eventManager |
42
|
|
|
* @param RedisDriver $redisDriver |
43
|
|
|
* @param Broadcast $broadcast |
44
|
|
|
* @param string $logDir |
45
|
|
|
*/ |
46
|
|
|
public function __construct(EventManager $eventManager, RedisDriver $redisDriver, Broadcast $broadcast, string $logDir) |
47
|
|
|
{ |
48
|
|
|
$this->eventManager = $eventManager; |
49
|
|
|
$this->redisDriver = $redisDriver; |
50
|
|
|
$this->broadcast = $broadcast; |
51
|
|
|
$this->logDir = $logDir; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get node js process |
56
|
|
|
* |
57
|
|
|
* @param string $server |
58
|
|
|
* @param string $ssl |
59
|
|
|
* |
60
|
|
|
* @return Process |
61
|
|
|
*/ |
62
|
|
|
public function nodeJs(string $server, string $ssl = ''): Process |
63
|
|
|
{ |
64
|
|
|
$cmd = sprintf('node %s/%s', realpath(dirname(__FILE__) . '/../Server'), 'index.js'); |
65
|
|
|
|
66
|
|
|
$connection = json_encode(array_filter([ |
67
|
|
|
'host' => $this->redisDriver->getHost(), |
68
|
|
|
'port' => $this->redisDriver->getPort(), |
69
|
|
|
'password' => $this->redisDriver->getPassword(), |
70
|
|
|
])); |
71
|
|
|
|
72
|
|
|
$args = array_filter([ |
73
|
|
|
'server' => $server, |
74
|
|
|
'pub' => $connection, |
75
|
|
|
'sub' => $connection, |
76
|
|
|
'channels' => implode(',', $this->broadcast->channels()), |
77
|
|
|
'nsp' => getenv('SOCKET_IO_NSP'), |
78
|
|
|
'ssl' => empty($ssl) ? null : $ssl, |
79
|
|
|
'runtime' => $this->logDir, |
80
|
|
|
], 'strlen'); |
81
|
|
|
foreach ($args as $key => $value) { |
82
|
|
|
$cmd .= ' -' . $key . '=\'' . $value . '\''; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$process = new Process($cmd); |
86
|
|
|
|
87
|
|
|
return $process; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Start predis |
92
|
|
|
*/ |
93
|
|
|
public function predis(SymfonyStyle $io) |
94
|
|
|
{ |
95
|
|
|
$pubSubLoop = function () use ($io) { |
96
|
|
|
/** @var \Predis\Client $client */ |
97
|
|
|
$client = $this->redisDriver->getClient(true); |
98
|
|
|
|
99
|
|
|
// Initialize a new pubsub consumer. |
100
|
|
|
$pubSub = $client->pubSubLoop(); |
101
|
|
|
|
102
|
|
|
$channels = []; |
103
|
|
|
foreach ($this->broadcast->channels() as $key => $channel) { |
104
|
|
|
$channels[$key] = $channel . '.io'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Subscribe to your channels |
108
|
|
|
$pubSub->subscribe(array_merge(['control_channel'], $channels)); |
109
|
|
|
|
110
|
|
|
// Start processing the pubsup messages. Open a terminal and use redis-cli |
111
|
|
|
// to push messages to the channels. Examples: |
112
|
|
|
// ./redis-cli PUBLISH notifications "this is a test" |
113
|
|
|
// ./redis-cli PUBLISH control_channel quit_loop |
114
|
|
|
foreach ($pubSub as $message) { |
|
|
|
|
115
|
|
|
switch ($message->kind) { |
116
|
|
|
case 'subscribe': |
117
|
|
|
$io->success("Subscribed to {$message->channel}"); |
118
|
|
|
break; |
119
|
|
|
case 'message': |
120
|
|
|
if ('control_channel' == $message->channel) { |
121
|
|
|
if ('quit_loop' == $message->payload) { |
122
|
|
|
$io->success("Aborting pubsub loop...\n"); |
123
|
|
|
$pubSub->unsubscribe(); |
124
|
|
|
} else { |
125
|
|
|
$io->success("Received an unrecognized command: {$message->payload}\n"); |
126
|
|
|
} |
127
|
|
|
} else { |
128
|
|
|
$payload = json_decode($message->payload, true); |
129
|
|
|
$data = $payload['data'] ?? []; |
130
|
|
|
|
131
|
|
|
$this->broadcast->on($payload['name'], $data); |
132
|
|
|
} |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Always unset the pubsub consumer instance when you are done! The |
138
|
|
|
// class destructor will take care of cleanups and prevent protocol |
139
|
|
|
// desynchronizations between the client and the server. |
140
|
|
|
unset($pubSub); |
141
|
|
|
}; |
142
|
|
|
|
143
|
|
|
// Auto recconnect on redis timeout |
144
|
|
|
try { |
145
|
|
|
$pubSubLoop(); |
146
|
|
|
} catch (ConnectionException $e) { |
147
|
|
|
$pubSubLoop(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: