Skip to content

Commit

Permalink
Various improvements/bugfixes on client disconnect.
Browse files Browse the repository at this point in the history
  • Loading branch information
nekudo committed Feb 19, 2012
1 parent 8a530d4 commit 654d7c5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 31 deletions.
21 changes: 15 additions & 6 deletions server/lib/WebSocket/Application/StatusApplication.php
@@ -1,5 +1,4 @@
<?php

namespace WebSocket\Application;

/**
Expand All @@ -13,6 +12,7 @@ class StatusApplication extends Application
private $_clients = array();
private $_serverClients = array();
private $_serverInfo = array();
private $_serverClientCount = 0;


public function onConnect($client)
Expand Down Expand Up @@ -47,12 +47,12 @@ public function setServerInfo($serverInfo)
public function clientConnected($ip, $port)
{
$this->_serverClients[$port] = $ip;

$this->_serverClientCount++;
$this->statusMsg('Client connected: ' .$ip.':'.$port);
$data = array(
'ip' => $ip,
'port' => $port,
'clientCount' => count($this->_serverClients),
'clientCount' => $this->_serverClientCount,
);
$encodedData = $this->_encodeData('clientConnected', $data);
$this->_sendAll($encodedData);
Expand All @@ -61,10 +61,11 @@ public function clientConnected($ip, $port)
public function clientDisconnected($ip, $port)
{
unset($this->_serverClients[$port]);
$this->_serverClientCount--;
$this->statusMsg('Client disconnected: ' .$ip.':'.$port);
$data = array(
'port' => $port,
'clientCount' => count($this->_serverClients),
'clientCount' => $this->_serverClientCount,
);
$encodedData = $this->_encodeData('clientDisconnected', $data);
$this->_sendAll($encodedData);
Expand All @@ -77,7 +78,7 @@ public function clientActivity($port)
}

public function statusMsg($text, $type = 'info')
{
{
$data = array(
'type' => $type,
'text' => '['. strftime('%m-%d %H:%M', time()) . '] ' . $text,
Expand All @@ -88,6 +89,10 @@ public function statusMsg($text, $type = 'info')

private function _sendServerinfo($client)
{
if(count($this->_clients) < 1)
{
return false;
}
$currentServerInfo = $this->_serverInfo;
$currentServerInfo['clientCount'] = count($this->_serverClients);
$currentServerInfo['clients'] = $this->_serverClients;
Expand All @@ -96,7 +101,11 @@ private function _sendServerinfo($client)
}

private function _sendAll($encodedData)
{
{
if(count($this->_clients) < 1)
{
return false;
}
foreach($this->_clients as $sendto)
{
$sendto->send($encodedData);
Expand Down
2 changes: 1 addition & 1 deletion server/lib/WebSocket/Connection.php
Expand Up @@ -245,7 +245,7 @@ private function handle($data)
}

public function send($payload, $type = 'text', $masked = true)
{
{
$encodedData = $this->hybi10Encode($payload, $type, $masked);
if(!$this->server->writeBuffer($this->socket, $encodedData, strlen($encodedData)))
{
Expand Down
41 changes: 22 additions & 19 deletions server/lib/WebSocket/Server.php
Expand Up @@ -81,7 +81,8 @@ public function run()

if($bytes === 0)
{
$client->onDisconnect();
//$client->onDisconnect();
$this->removeClientOnError($client);
continue;
}
elseif($data === false)
Expand Down Expand Up @@ -160,13 +161,9 @@ public function log($message, $type = 'info')
*/
public function removeClientOnClose($client)
{
// trigger status application:
if($this->getApplication('status') !== false)
{
$this->getApplication('status')->clientDisconnected($client->getClientIp(), $client->getClientPort());
}

$clientId = $client->getClientId();
$clientIp = $client->getClientIp();
$clientPort = $client->getClientPort();
$resource = $client->getClientSocket();

$this->_removeIpFromStorage($client->getClientIp());
Expand All @@ -176,40 +173,46 @@ public function removeClientOnClose($client)
}
unset($this->clients[(int)$resource]);
$index = array_search($resource, $this->allsockets);
unset($this->allsockets[$index]);
unset($client, $clientId);
unset($this->allsockets[$index], $client);

// trigger status application:
if($this->getApplication('status') !== false)
{
$this->getApplication('status')->clientDisconnected($clientIp, $clientPort);
}
unset($clientId, $clientIp, $clientPort, $resource);
}

/**
* Removes a client and all references in case of timeout/error.
* @param object $client The client object to remove.
*/
public function removeClientOnError($client)
{
// trigger status application:
if($this->getApplication('status') !== false)
{
$this->getApplication('status')->clientDisconnected($client->getClientIp(), $client->getClientPort());
}

// remove reference in clients app:
{ // remove reference in clients app:
if($client->getClientApplication() !== false)
{
$client->getClientApplication()->onDisconnect($client);
}

$resource = $client->getClientSocket();
$clientId = $client->getClientId();
$clientIp = $client->getClientIp();
$clientPort = $client->getClientPort();
$this->_removeIpFromStorage($client->getClientIp());
if(isset($this->_requestStorage[$clientId]))
{
unset($this->_requestStorage[$clientId]);
}
unset($this->clients[(int)$resource]);
$index = array_search($resource, $this->allsockets);
unset($this->allsockets[$index]);
unset($client, $clientId, $resource);
unset($this->allsockets[$index], $client);

// trigger status application:
if($this->getApplication('status') !== false)
{
$this->getApplication('status')->clientDisconnected($clientIp, $clientPort);
}
unset($resource, $clientId, $clientIp, $clientPort);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions server/lib/WebSocket/Socket.php
Expand Up @@ -113,13 +113,13 @@ protected function readBuffer($resource)
do
{
if(feof($resource))
{
{
return false;
}
$result = fread($resource, $buffsize);
if($result === false || feof($resource))
{
return false;
{
return false;
}
$buffer .= $result;
$metadata = stream_get_meta_data($resource);
Expand All @@ -132,11 +132,11 @@ protected function readBuffer($resource)

// method originally found in phpws project:
public function writeBuffer($resource, $string)
{
{
$stringLength = strlen($string);
for($written = 0; $written < $stringLength; $written += $fwrite)
{
$fwrite = fwrite($resource, substr($string, $written));
$fwrite = fwrite($resource, substr($string, $written));
if($fwrite === false)
{
return false;
Expand Down

0 comments on commit 654d7c5

Please sign in to comment.