text changes to registration mail content
[namibia] / daemon / SocketApp.php
1 <?php
2 namespace Daemon;
3
4
5 use Ratchet\MessageComponentInterface;
6 use Ratchet\ConnectionInterface;
7
8 /**
9  * Meet Damien, our client connectivity application.
10  * @author andre.fourie
11  */
12 class Damien implements MessageComponentInterface
13 {
14
15         /**
16          * General debugging switch.
17          * @var boolean
18          */
19         protected $debug = false;
20         /**
21          * Worker server ip address.
22          * @var string
23          */
24         protected $workerIp = '127.0.0.1';
25         /**
26          * Client websocket connections.
27          * @var array
28          */
29         protected $clients;
30         /**
31          * Worker websocket connections.
32          * @var array
33          */
34         protected $workers;
35         /**
36          * Gearman connection.
37          * @var \GearmanClient
38          */
39         protected $gearClient;
40
41
42         /**
43          * Setup the basics.
44          */
45         public function __construct()
46         {
47                 $this->clients    = array();
48                 $this->workers    = array();
49                 $this->gearClient = new \GearmanClient();
50                 $this->gearClient->addServer();
51         }
52
53
54
55
56         ### ------------------------------------------------------ CONNECTION EVENT HANDLING
57         /**
58          * Handle new websocket connections.
59          * @param  ConnectionInterface $from Websocket connection interface
60          * @return null
61          */
62         public function onOpen(ConnectionInterface $conn)
63         {
64                 #-> Store the new connection.
65                 $this->clients[$conn->resourceId] = $conn;
66                 if ($this->debug)
67                 {
68                         $this->gearClient->doBackground(
69                                 'Log',
70                                 json_encode(
71                                         array(
72                                                 'message' => "\nConnections: " . count($this->clients) . "\n"
73                                                              . 'Memory usage: ' . round(memory_get_usage(true) / 1048576, 2) . 'mb'
74                                         )
75                                 )
76                         );
77                 }
78         }
79
80         /**
81          * Handle received websocket messages.
82          * @param  ConnectionInterface $conn    Websocket connection interface
83          * @param  string              $message Message from connected client
84          * @return null
85          */
86         public function onMessage(ConnectionInterface $conn, $message)
87         {
88                 $workload = json_decode($message, true);
89                 if (isset($workload['ping']))
90                 {
91                         $conn->send('{"pong":true}');
92                 }
93         }
94
95         /**
96          * Handle websocket connection closure.
97          * @param  ConnectionInterface $conn Websocket connection interface
98          * @return null
99          */
100         public function onClose(ConnectionInterface $conn)
101         {
102                 #-> Client connection closed, do some cleanup.
103                 unset($this->clients[$conn->resourceId]);
104                 gc_collect_cycles();
105                 if ($this->debug)
106                 {
107                         $this->gearClient->doBackground(
108                                 'Log',
109                                 json_encode(
110                                         array(
111                                                 'message' => "\nConnections: " . count($this->clients) . "\n"
112                                                              . 'Memory usage: ' . round(memory_get_usage(true) / 1048576, 2) . 'mb'
113                                         )
114                                 )
115                         );
116                 }
117         }
118
119         /**
120          * Handle websocket connection error.
121          * @param  ConnectionInterface $conn Websocket connection interface
122          * @param  \Exception          $e    The exception instance
123          * @return null
124          */
125         public function onError(ConnectionInterface $conn, \Exception $e)
126         {
127                 #-> Nasty connection error, close the connection.
128                 if ($this->debug)
129                 {
130                         $this->gearClient->doBackground(
131                                 'Log',
132                                 json_encode(
133                                         array(
134                                                 'message' => 'SocketApp Connection Error: ' . $e->getMessage()
135                                         )
136                                 )
137                         );
138                 }
139                 $conn->close();
140         }
141
142         /**
143          * Handle new ZeroMQ message.
144          * @param  String $message Serialized message from ZeroMQ
145          * @return null
146          */
147         public function onQueueMessage($message)
148         {
149                 #-> Broadcast.
150                 $start = microtime(true);
151                 foreach ($this->clients as $id => $conn)
152                 {
153                         $conn->send($message);
154                 }
155                 $time = round(microtime(true) - $start, 3);
156                 $this->gearClient->doBackground(
157                         'Log',
158                         json_encode(
159                                 array(
160                                         'message' => 'Connections: ' . count($this->clients) . '; Cost: ' . $time
161                                 )
162                         )
163                 );
164         }
165
166
167 }