Permalink
Browse files

Added appdata and removed trackers.

  • Loading branch information...
cubiclesoft committed Aug 21, 2018
1 parent 2372d96 commit abef4863711213ff3597793f7a2d6a6e0548679d
Showing with 59 additions and 26 deletions.
  1. +25 −0 docs/web_server.md
  2. +8 −19 tests/test_web_server.php
  3. +3 −7 tests/test_websocket_server.php
  4. +18 −0 web_server.php
  5. +5 −0 websocket_server.php
View
@@ -112,6 +112,17 @@ This function sets the cache directory or disables the disk cache. The default
When the disk cache is enabled, any key-value pair in $client->requestvars may point at an instance of WebServer_TempFile instead of a string if any given value exceeds the hard limit.
WebServer::GetCacheDir()
------------------------
Access: public
Parameters: None.
Returns: A string containing the current cache directory or a boolean of false if not set.
This function retrieves the caching directory set previously by `SetCacheDir()`.
WebServer::Start($host, $port, $sslopts = false)
------------------------------------------------
@@ -395,6 +406,20 @@ Returns: Nothing.
This protected function handles new incoming connections in Wait(). Can be overridden in a derived class to provide alternate functionality.
WebServer::HandleResponseCompleted($id, $result)
------------------------------------------------
Access: protected
Parameters:
* $id - The client ID.
* $result - A standard array of information.
Returns: Nothing.
This protected function is called at the end of each sent response. Can be overridden in a derived class to do things such as gather statistics.
WebServer::DetachClient($id)
----------------------------
View
@@ -58,9 +58,6 @@ function ProcessAPI($url, $data)
return $result;
}
$tracker = array();
$tracker2 = array();
do
{
// Implement the stream_select() call directly since multiple server instances are involved.
@@ -79,15 +76,15 @@ function ProcessAPI($url, $data)
// Do something with active clients.
foreach ($result["clients"] as $id => $client)
{
if (!isset($tracker[$id]))
if ($client->appdata === false)
{
echo "Client ID " . $id . " connected.\n";
$tracker[$id] = array("validapikey" => false);
$client->appdata = array("validapikey" => false);
}
// Example of checking for an API key.
if (!$tracker[$id]["validapikey"] && isset($client->requestvars["apikey"]) && $client->requestvars["apikey"] === "123456789101112")
if (!$client->appdata["validapikey"] && isset($client->requestvars["apikey"]) && $client->requestvars["apikey"] === "123456789101112")
{
echo "Valid API key used.\n";
@@ -100,13 +97,13 @@ function ProcessAPI($url, $data)
$client->SetHTTPOptions($options);
}
$tracker[$id]["validapikey"] = true;
$client->appdata["validapikey"] = true;
}
// Wait until the request is complete before fully processing inputs.
if ($client->requestcomplete)
{
if (!$tracker[$id]["validapikey"])
if (!$client->appdata["validapikey"])
{
echo "Missing API key.\n";
@@ -122,10 +119,6 @@ function ProcessAPI($url, $data)
if ($id2 !== false)
{
echo "Client ID " . $id . " upgraded to WebSocket. WebSocket client ID is " . $id2 . ".\n";
$tracker2[$id2] = $tracker[$id];
unset($tracker[$id]);
}
else
{
@@ -156,15 +149,13 @@ function ProcessAPI($url, $data)
// Do something with removed clients.
foreach ($result["removed"] as $id => $result2)
{
if (isset($tracker[$id]))
if ($result2["client"]->appdata !== false)
{
echo "Client ID " . $id . " disconnected.\n";
// echo "Client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
unset($tracker[$id]);
}
}
@@ -198,15 +189,13 @@ function ProcessAPI($url, $data)
// Do something with removed clients.
foreach ($result["removed"] as $id => $result2)
{
if (isset($tracker2[$id]))
if ($result2["client"]->appdata !== false)
{
echo "WebSocket client ID " . $id . " disconnected.\n";
// echo "Client ID " . $id . " disconnected. Reason:\n";
// echo "WebSocket client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
unset($tracker2[$id]);
}
}
} while (1);
@@ -57,16 +57,14 @@ function ProcessAPI($url, $data)
return $result;
}
$tracker = array();
do
{
$result = $wsserver->Wait();
// Do something with active clients.
foreach ($result["clients"] as $id => $client)
{
if (!isset($tracker[$id]))
if ($client->appdata === false)
{
echo "Client ID " . $id . " connected.\n";
@@ -81,7 +79,7 @@ function ProcessAPI($url, $data)
echo "Valid API key used.\n";
$tracker[$id] = array();
$client->appdata = array();
}
// This example processes the client input (add/multiply two numbers together) and sends back the result.
@@ -108,15 +106,13 @@ function ProcessAPI($url, $data)
// Do something with removed clients.
foreach ($result["removed"] as $id => $result2)
{
if (isset($tracker[$id]))
if ($result2["client"]->appdata !== false)
{
echo "Client ID " . $id . " disconnected.\n";
// echo "Client ID " . $id . " disconnected. Reason:\n";
// var_dump($result2["result"]);
// echo "\n";
unset($tracker[$id]);
}
}
} while (1);
View
@@ -80,6 +80,11 @@ public function SetCacheDir($cachedir)
$this->cachedir = $cachedir;
}
public function GetCacheDir()
{
return $this->cachedir;
}
// Starts the server on the host and port.
// $host is usually 0.0.0.0 or 127.0.0.1 for IPv4 and [::0] or [::1] for IPv6.
public function Start($host, $port, $sslopts = false)
@@ -560,6 +565,9 @@ public function InitNewClient()
$client->currfile = false;
$client->files = array();
// Intended for application storage.
$client->appdata = false;
$this->initclients[$this->nextclientid] = $client;
$this->nextclientid++;
@@ -585,6 +593,10 @@ protected function HandleNewConnections(&$readfps, &$writefps)
}
}
protected function HandleResponseCompleted($id, $result)
{
}
// Handles new connections, the initial conversation, basic packet management, rate limits, and timeouts.
// Can wait on more streams than just sockets and/or more sockets. Useful for waiting on other resources.
// 'http_s' and the 'http_c_' prefix are reserved.
@@ -761,6 +773,8 @@ public function Wait($timeout = false, $readfps = array(), $writefps = array(),
}
else if ($client->keepalive && $client->requests < $this->maxrequests)
{
$this->HandleResponseCompleted($id, $result2);
// Reset client for another request.
$client->mode = "init_request";
$client->readdata = $client->httpstate["nextread"];
@@ -792,13 +806,17 @@ public function Wait($timeout = false, $readfps = array(), $writefps = array(),
}
else
{
$this->HandleResponseCompleted($id, $result2);
$result["removed"][$id] = array("result" => array("success" => true), "client" => $client);
$this->RemoveClient($id);
}
}
else if ($result2["errorcode"] !== "no_data")
{
$this->HandleResponseCompleted($id, $result2);
$result["removed"][$id] = array("result" => $result2, "client" => $client);
$this->RemoveClient($id);
View
@@ -146,6 +146,9 @@ protected function InitNewClient($fp)
$client->websocket = false;
$client->fp = $fp;
// Intended for application storage.
$client->appdata = false;
$this->clients[$this->nextclientid] = $client;
$this->nextclientid++;
@@ -488,6 +491,8 @@ public function ProcessWebServerClientUpgrade($webserver, $client)
$client2->path = $path;
$client2->url = "ws://" . (isset($client->headers["Host"]) ? $client->headers["Host"] : "localhost") . $path;
$client2->appdata = $client->appdata;
$this->ProcessInitialResponse($method, $path, $client2);
return $client2->id;

0 comments on commit abef486

Please sign in to comment.