-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.php
40 lines (31 loc) · 932 Bytes
/
controller.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
<?php
require 'vendor/autoload.php';
use Ratchet\Client\WebSocket;
use Ratchet\Client\Connector;
$command_file = "D:/AllFiles/wamp64/www/command.txt";
if (isset($_GET['cmd'])) {
$command = $_GET['cmd'];
if ($command === "ON" || $command === "OFF") {
file_put_contents($command_file, $command);
echo "Command set to: $command";
notifyWebSocketClients($command);
} else {
echo "Invalid command!";
}
} else {
echo "No command received!";
}
function notifyWebSocketClients($command) {
$connector = new Connector();
$connector('ws://localhost:8080')->then(
function (WebSocket $conn) use ($command) {
$conn->send($command);
echo "Command sent to WebSocket: $command\n";
$conn->close();
},
function ($e) {
echo "Could not connect to WebSocket: {$e->getMessage()}\n";
}
);
}
?>