Skip to content

Commit

Permalink
Adds a connection factory method to the server
Browse files Browse the repository at this point in the history
Abstracting the creation of connection objects to a protected method
is nicer for subclasses. Subclasses can override createConnection()
to replace the Connection class.

Without this abstraction, the Server and Connection classes are tightly
coupled, and the whole lengthy run() method would need to be copy-pasted
into the subclass.
  • Loading branch information
dominics committed Jun 5, 2012
1 parent 7283f34 commit 8b354a5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion server/lib/WebSocket/Server.php
Expand Up @@ -27,6 +27,17 @@ public function __construct($host = 'localhost', $port = 8000, $ssl = false)
$this->log('Server created');
}

/**
* Creates a connection from a socket resource
*
* @param resource $resource A socket resource
* @return Connection
*/
protected function createConnection($resource)
{
return new Connection($this, $resource);
}

/**
* Main server method. Listens for connections, handles connectes/disconnectes, e.g.
*/
Expand All @@ -47,7 +58,7 @@ public function run()
}
else
{
$client = new Connection($this, $ressource);
$client = $this->createConnection($ressource);
$this->clients[(int)$ressource] = $client;
$this->allsockets[] = $ressource;

Expand Down

0 comments on commit 8b354a5

Please sign in to comment.