Skip to content
This repository has been archived by the owner on Sep 7, 2019. It is now read-only.

Commit

Permalink
Use composer autoloader
Browse files Browse the repository at this point in the history
No more say(), but ZF2 logger.
Also moved the event loop to seperate class. Opens doors to more complex applications and perhaps a php libevent backend later
  • Loading branch information
Devristo committed Nov 21, 2013
1 parent 20a58c8 commit 703ef1e
Show file tree
Hide file tree
Showing 16 changed files with 304 additions and 338 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/.idea
/phpunit.phar
/phpunit.phar
/vendor
/composer.lock
136 changes: 0 additions & 136 deletions SplClassLoader.php

This file was deleted.

5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"psr-0": {
"Devristo\\Phpws\\": "src/"
}
},
"require": {
"zendframework/zend-log": "2.*"
}
}
}
30 changes: 14 additions & 16 deletions demo.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/php -q
<?php

require_once("./SplClassLoader.php");

$loader = new SplClassLoader("Devristo\\Phpws","src");
$loader->register();
require_once(__DIR__."/vendor/autoload.php");


// Run from command prompt > php demo.php
Expand All @@ -26,13 +23,13 @@
class DemoEchoHandler extends WebSocketUriHandler {

public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->say("[ECHO] " . strlen($msg->getData()) . " bytes");
$this->logger->notice("[ECHO] " . strlen($msg->getData()) . " bytes");
// Echo
$user->sendMessage($msg);
}

public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $obj) {
$this->say("[DEMO] Admin TEST received!");
$this->logger->notice("[DEMO] Admin TEST received!");

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
Expand All @@ -53,35 +50,36 @@ class DemoSocketServer implements IWebSocketServerObserver {
protected $server;

public function __construct() {
$this->server = new WebSocketServer("tcp://0.0.0.0:12345", 'superdupersecretkey');
$logger = new \Zend\Log\Logger();
$logger->addWriter(new Zend\Log\Writer\Stream("php://output"));

$this->logger = $logger;

$this->server = new WebSocketServer("tcp://0.0.0.0:12345", $logger);
$this->server->addObserver($this);

$this->server->addUriHandler("echo", new DemoEchoHandler());
$this->server->addUriHandler("echo", new DemoEchoHandler($logger));
}

public function onConnect(IWebSocketConnection $user) {
$this->say("[DEMO] {$user->getId()} connected");
$this->logger->notice("[DEMO] {$user->getId()} connected");
}

public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
//$this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'");
//$this->logger->notice("[DEMO] {$user->getId()} says '{$msg->getData()}'");
}

public function onDisconnect(IWebSocketConnection $user) {
$this->say("[DEMO] {$user->getId()} disconnected");
$this->logger->notice("[DEMO] {$user->getId()} disconnected");
}

public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->say("[DEMO] Admin Message received!");
$this->logger->notice("[DEMO] Admin Message received!");

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
}

public function say($msg) {
echo "$msg \r\n";
}

public function run() {
$this->server->run();
}
Expand Down
30 changes: 14 additions & 16 deletions demo_ssl.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#!/php -q
<?php

require_once("./SplClassLoader.php");

$loader = new SplClassLoader("Devristo\\Phpws","src");
$loader->register();
require_once("vendor/autoload.php");


// Run from command prompt > php demo.php
Expand All @@ -26,13 +23,13 @@
class DemoSslEchoHandler extends WebSocketUriHandler {

public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->say("[ECHO] {$msg->getData()}");
$this->logger->notice("[ECHO] {$msg->getData()}");
// Echo
$user->sendMessage($msg);
}

public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $obj) {
$this->say("[DEMO] Admin TEST received!");
$this->logger->notice("[DEMO] Admin TEST received!");

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
Expand All @@ -53,10 +50,15 @@ class DemoSslSocketServer implements IWebSocketServerObserver {
protected $server;

public function __construct() {
$this->server = new WebSocketServer("ssl://0.0.0.0:12345");
$logger = new \Zend\Log\Logger();
$logger->addWriter(new Zend\Log\Writer\Stream("php://output"));

$this->logger = $logger;

$this->server = new WebSocketServer("ssl://0.0.0.0:12345", $logger);
$this->server->addObserver($this);

$this->server->addUriHandler("echo", new DemoSslEchoHandler());
$this->server->addUriHandler("echo", new DemoSslEchoHandler($logger));

$this->setupSSL();
}
Expand All @@ -78,28 +80,24 @@ public function setupSSL() {
}

public function onConnect(IWebSocketConnection $user) {
$this->say("[DEMO] {$user->getId()} connected");
$this->logger->notice("[DEMO] {$user->getId()} connected");
}

public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->say("[DEMO] {$user->getId()} says '{$msg->getData()}'");
$this->logger->notice("[DEMO] {$user->getId()} says '{$msg->getData()}'");
}

public function onDisconnect(IWebSocketConnection $user) {
$this->say("[DEMO] {$user->getId()} disconnected");
$this->logger->notice("[DEMO] {$user->getId()} disconnected");
}

public function onAdminMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
$this->say("[DEMO] Admin Message received!");
$this->logger->notice("[DEMO] Admin Message received!");

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
$user->sendFrame($frame);
}

public function say($msg) {
echo "$msg \r\n";
}

public function run() {
$this->server->run();
}
Expand Down
16 changes: 13 additions & 3 deletions src/Devristo/Phpws/Protocol/WebSocketConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@
use Devristo\Phpws\Framing\IWebSocketFrame;
use Devristo\Phpws\Messaging\IWebSocketMessage;
use Devristo\Phpws\Protocol\WebSocketStream;
use Zend\Log\LoggerAwareInterface;
use Zend\Log\LoggerInterface;

abstract class WebSocketConnection implements IWebSocketConnection
abstract class WebSocketConnection implements IWebSocketConnection, LoggerAwareInterface
{

protected $_headers = array();

/**
* @var LoggerInterface
*/
protected $logger;

/**
*
* @var WebSocketStream
Expand All @@ -34,12 +41,12 @@ public function __construct(WebSocketStream $socket, array $headers)

public function getIp()
{
return stream_socket_get_name($this->_socket->getResource(), true);
return stream_socket_get_name($this->_socket->getSocket(), true);
}

public function getId()
{
return (int)$this->_socket->getResource();
return (int)$this->_socket->getSocket();
}

public function sendFrame(IWebSocketFrame $frame)
Expand Down Expand Up @@ -154,4 +161,7 @@ public function getSocket()
return $this->_socket;
}

public function setLogger(LoggerInterface $logger){
$this->logger = $logger;
}
}
7 changes: 5 additions & 2 deletions src/Devristo/Phpws/Protocol/WebSocketConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,30 @@
use Devristo\Phpws\Protocol\WebSocketConnectionHybi;
use Devristo\Phpws\Protocol\WebSocketConnectionRole;
use Devristo\Phpws\Protocol\WebSocketStream;
use Zend\Log\LoggerInterface;

class WebSocketConnectionFactory
{

public static function fromSocketData(WebSocketStream $socket, $data)
public static function fromSocketData(WebSocketStream $socket, $data, LoggerInterface $logger)
{
$headers = self::parseHeaders($data);

if (isset($headers['Sec-Websocket-Key1'])) {
$s = new WebSocketConnectionHixie($socket, $headers, $data);
$s->setLogger($logger);
$s->sendHandshakeResponse();
} else if (strpos($data, '<policy-file-request/>') === 0) {
$s = new WebSocketConnectionFlash($socket, $data);
$s->setLogger($logger);
} else {
$s = new WebSocketConnectionHybi($socket, $headers);
$s->setLogger($logger);
$s->sendHandshakeResponse();
}

$s->setRole(WebSocketConnectionRole::SERVER);


return $s;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Devristo/Phpws/Protocol/WebSocketConnectionHybi.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function sendHandshakeResponse()

$this->_socket->write($response);

echo "HYBI Response SENT!\n";
$this->logger->debug("HYBI Response SENT!");
}

private static function calcHybiResponse($challenge)
Expand Down
Loading

0 comments on commit 703ef1e

Please sign in to comment.