Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Application/StatusApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function onConnect(Connection $client): void
{
$id = $client->getClientId();
$this->clients[$id] = $client;
$this->sendServerinfo($client);
// no longer send serverinfo at connect ( d.roche )
//$this->sendServerinfo($client);
}

/**
Expand All @@ -70,7 +71,10 @@ public function onDisconnect(Connection $client): void
*/
public function onData(string $data, Connection $client): void
{
// currently not in use...
// modified by d.roche to send server info on any received data...
foreach ($this->clients as $cli1) {
$this->sendServerinfo($cli1);
}
}

/**
Expand All @@ -97,7 +101,7 @@ public function setServerInfo(array $serverInfo): bool
*/
public function clientConnected(string $ip, int $port): void
{
$this->serverClients[$port] = $ip;
$this->serverClients[$ip.":".$port] = date("U");
$this->serverClientCount++;
$this->statusMsg('Client connected: ' . $ip . ':' . $port);
$data = [
Expand All @@ -118,10 +122,10 @@ public function clientConnected(string $ip, int $port): void
*/
public function clientDisconnected(string $ip, int $port): void
{
if (!isset($this->serverClients[$port])) {
if (!isset($this->serverClients[$ip.":".$port])) {
return;
}
unset($this->serverClients[$port]);
unset($this->serverClients[$ip.":".$port]);
$this->serverClientCount--;
$this->statusMsg('Client disconnected: ' . $ip . ':' . $port);
$data = [
Expand Down
47 changes: 47 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,53 @@ public function sendData(string $data, string $type = 'text', bool $masked = tru
return true;
}

/**
* wait for data
* added by [email protected]
*
* @param int $timeout ( sec )
* @return bool
*/
public function wait(int $timeout = null): bool
{
if ($this->connected === false) {
trigger_error("Not connected", E_USER_WARNING);
return false;
}
$rsocks = Array($this->socket);
$changed = @stream_select($rsocks, $write = null, $except = null, $timeout);
if ( $changed > 0 ) {
return(true);
} else {
return(false);
}
}

/**
* read data from remote server.
* added by [email protected]
*
* @param int $len
* @return string
*/
public function readData(int $len = 4096): string
{
if ($this->connected === false) {
trigger_error("Not connected", E_USER_WARNING);
return false;
}
$ret = '';

$buffer = fread($this->socket, $len);
if ( ! empty($buffer) ) {
$decbuff = $this->hybi10Decode($buffer);
if ( $decbuff['type'] == "text" ) {
$ret = $decbuff['payload'];
}
}
return $ret;
}

/**
* Connects to a websocket server.
*
Expand Down