Permalink
Browse files

Adds a connection factory method to the server

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...
1 parent 7283f34 commit 8b354a5f6b08ff1fd80bc8203f38dfb34395a921 @dominics dominics committed Jun 5, 2012
Showing with 12 additions and 1 deletion.
  1. +12 −1 server/lib/WebSocket/Server.php
@@ -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.
*/
@@ -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;

0 comments on commit 8b354a5

Please sign in to comment.