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

Commit

Permalink
Breaking changes, UriHandlers are now managed by the ClientRouter. Be…
Browse files Browse the repository at this point in the history
…tter PSR-2 compliance. Some changes to the examples
  • Loading branch information
Devristo committed Nov 26, 2013
1 parent fc5309f commit 7a3b0ee
Show file tree
Hide file tree
Showing 33 changed files with 662 additions and 1,458 deletions.
141 changes: 0 additions & 141 deletions TcpStream.php

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"require": {
"zendframework/zend-log": "2.*",
"zendframework/zend-eventmanager": "2.2.5"
"react/socket": "v0.3.2",
"react/socket-client": "v0.3.2"
}
}
File renamed without changes.
41 changes: 28 additions & 13 deletions demo.php → examples/demo.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#!/php -q
<?php

require_once(__DIR__."/vendor/autoload.php");
require_once(__DIR__ . "/vendor/autoload.php");


// Run from command prompt > php demo.php
use Devristo\Phpws\Framing\WebSocketFrame;
use Devristo\Phpws\Framing\WebSocketOpcode;
use Devristo\Phpws\Messaging\IWebSocketMessage;
use Devristo\Phpws\Protocol\IWebSocketConnection;
use Devristo\Phpws\Protocol\WebSocketConnectionInterface;
use Devristo\Phpws\Server\IWebSocketServerObserver;
use Devristo\Phpws\Server\UriHandler\WebSocketUriHandler;
use Devristo\Phpws\Server\WebSocketServer;
Expand All @@ -22,13 +22,25 @@
*/
class DemoEchoHandler extends WebSocketUriHandler {

public function onMessage(IWebSocketConnection $user, IWebSocketMessage $msg) {
public function __construct(\Zend\Log\LoggerInterface $logger, \React\EventLoop\LoopInterface $loop){
parent::__construct($logger);

$that = $this;

$loop->addPeriodicTimer(1, function() use ($that){
foreach($that->getConnections() as $client){
$client->sendString("Hello world!");
}
});
}

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

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

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
Expand All @@ -49,31 +61,31 @@ class DemoSocketServer implements IWebSocketServerObserver {
protected $debug = true;
protected $server;

public function __construct() {
public function __construct(\React\EventLoop\LoopInterface $loop) {
$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 = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);
$this->server->addObserver($this);

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

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

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

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

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

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
Expand All @@ -86,6 +98,9 @@ public function run() {

}

$loop = \React\EventLoop\Factory::create();

// Start server
$server = new DemoSocketServer();
$server->run();
$server = new DemoSocketServer($loop);
$server->run();
$loop->run();
File renamed without changes.
25 changes: 14 additions & 11 deletions demo_ssl.php → examples/demo_ssl.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Devristo\Phpws\Framing\WebSocketFrame;
use Devristo\Phpws\Framing\WebSocketOpcode;
use Devristo\Phpws\Messaging\IWebSocketMessage;
use Devristo\Phpws\Protocol\IWebSocketConnection;
use Devristo\Phpws\Protocol\WebSocketConnectionInterface;
use Devristo\Phpws\Server\IWebSocketServerObserver;
use Devristo\Phpws\Server\UriHandler\WebSocketUriHandler;
use Devristo\Phpws\Server\WebSocketServer;
Expand All @@ -22,13 +22,13 @@
*/
class DemoSslEchoHandler extends WebSocketUriHandler {

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

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

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
Expand All @@ -49,16 +49,16 @@ class DemoSslSocketServer implements IWebSocketServerObserver {
protected $debug = true;
protected $server;

public function __construct() {
public function __construct($loop) {
$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 = new WebSocketServer("ssl://0.0.0.0:12345", $loop, $logger);
$this->server->addObserver($this);

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

$this->setupSSL();
}
Expand All @@ -79,19 +79,19 @@ public function setupSSL() {
$this->server->setStreamContext($context);
}

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

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

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

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

$frame = WebSocketFrame::create(WebSocketOpcode::PongFrame);
Expand All @@ -104,6 +104,9 @@ public function run() {

}

$loop = \React\EventLoop\Factory::create();

// Start server
$server = new DemoSslSocketServer();
$server = new ProxyWebSocketServer($loop);
$server->run();
$loop->run();
31 changes: 31 additions & 0 deletions examples/echo_client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: Chris
* Date: 30-9-13
* Time: 21:05
*/

require_once(__DIR__ . "/../vendor/autoload.php");

$loop = \React\EventLoop\Factory::create();


$logger = new \Zend\Log\Logger();
$writer = new Zend\Log\Writer\Stream("php://output");
$logger->addWriter($writer);

$client = new \Devristo\Phpws\Client\WebSocket("ws://echo.websocket.org/?encoding=text", $loop, $logger);
$client->on("connected", function($headers) use ($logger, $client){
$logger->notice("Connected!");
$client->send("Hello world!");
});

$client->on("message", function($message) use ($client, $logger){
$logger->notice("Got message: ".$message->getData());
$client->close();
});


$client->open();
$loop->run();
File renamed without changes.
Loading

0 comments on commit 7a3b0ee

Please sign in to comment.