-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathtest_websocket_server.php
119 lines (92 loc) · 3.26 KB
/
test_websocket_server.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
// This is strictly a test of the WebSocketServer class which only implements WebSocket.
// For a more complete end-user experience, see 'test_web_server.php'.
if (!isset($_SERVER["argc"]) || !$_SERVER["argc"])
{
echo "This file is intended to be run from the command-line.";
exit();
}
// Temporary root.
$rootpath = str_replace("\\", "/", dirname(__FILE__));
require_once $rootpath . "/../websocket_server.php";
require_once $rootpath . "/../support/websocket.php";
require_once $rootpath . "/../support/http.php";
$wsserver = new WebSocketServer();
echo "Starting server...\n";
$result = $wsserver->Start("127.0.0.1", "5578");
if (!$result["success"])
{
var_dump($result);
exit();
}
echo "Ready.\n";
function ProcessAPI($url, $data)
{
if (!is_array($data)) return array("success" => false, "error" => "Data sent is not an array/object or was not able to be decoded.", "errorcode" => "invalid_data");
if (!isset($data["pre"]) || !isset($data["op"]) || !isset($data["post"])) return array("success" => false, "error" => "Missing required 'pre', 'op', or 'post' data entries.", "errorcode" => "missing_data");
// Sanitize inputs.
$data["pre"] = (double)$data["pre"];
$data["op"] = (string)$data["op"];
$data["post"] = (double)$data["post"];
$question = $data["pre"] . " " . $data["op"] . " " . $data["post"];
if ($data["op"] === "+") $answer = $data["pre"] + $data["post"];
else if ($data["op"] === "-") $answer = $data["pre"] - $data["post"];
else if ($data["op"] === "*") $answer = $data["pre"] * $data["post"];
else if ($data["op"] === "/" && $data["post"] != 0) $answer = $data["pre"] / $data["post"];
else $answer = "NaN";
$result = array(
"success" => true,
"response" => array(
"question" => $question,
"answer" => $answer
)
);
return $result;
}
do
{
$result = $wsserver->Wait();
// Do something with active clients.
foreach ($result["clients"] as $id => $client)
{
if ($client->appdata === false)
{
echo "Client ID " . $id . " connected.\n";
// Example of checking for an API key.
$url = HTTP::ExtractURL($client->url);
if (!isset($url["queryvars"]["apikey"]) || $url["queryvars"]["apikey"][0] !== "123456789101112")
{
$wsserver->RemoveClient($id);
continue;
}
echo "Valid API key used.\n";
$client->appdata = array();
}
// This example processes the client input (add/multiply two numbers together) and sends back the result.
$ws = $client->websocket;
$result2 = $ws->Read();
while ($result2["success"] && $result2["data"] !== false)
{
echo "Sending API response via WebSocket.\n";
// Attempt to normalize the input.
$data = json_decode($result2["data"]["payload"], true);
// Process the request.
$result3 = ProcessAPI($client->url, $data);
// Send the response.
$result2 = $ws->Write(json_encode($result3), $result2["data"]["opcode"]);
$result2 = $ws->Read();
}
}
// Do something with removed clients.
foreach ($result["removed"] as $id => $result2)
{
if ($result2["client"]->appdata !== false)
{
echo "Client ID " . $id . " disconnected.\n";
// echo "Client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
}
}
} while (1);
?>