-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
546 changed files
with
73,613 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Controllers\Websocket\BrowserClient; | ||
|
||
use Exception; | ||
|
||
/** | ||
* Class Process extends WebsocketServer to gain access to its methods | ||
*/ | ||
class Process extends \Controllers\Websocket\WebsocketServer | ||
{ | ||
/** | ||
* Reload containers for all browser clients | ||
*/ | ||
public function reload($socket) | ||
{ | ||
// Get all containers that need to be reloaded | ||
$containers = $this->layoutContainerReloadController->get(); | ||
|
||
// Quit if there are no containers to reload | ||
if (empty($containers)) { | ||
return; | ||
} | ||
|
||
// For each container, send a reload request to all browser clients | ||
foreach ($containers as $container) { | ||
$this->broadcast($socket, 'browser-client', array( | ||
'type' => 'reload-container', | ||
'container' => $container['Container'] | ||
)); | ||
} | ||
|
||
// Clean up the layout container state | ||
$this->layoutContainerReloadController->clean(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
namespace Controllers\Websocket; | ||
|
||
/** | ||
* Composer autoload | ||
*/ | ||
require ROOT . '/libs/vendor/autoload.php'; | ||
|
||
use Exception; | ||
use Ratchet\MessageComponentInterface; | ||
use Ratchet\ConnectionInterface; | ||
|
||
/** | ||
* Class Socker extends WebsocketServer to gain access to its methods | ||
*/ | ||
class Socket extends WebsocketServer implements MessageComponentInterface | ||
{ | ||
protected $clients; | ||
|
||
/** | ||
* Initialize socket | ||
* Basically like a constructor but to avoid conflicts with the parent constructor | ||
*/ | ||
public function initialize() | ||
{ | ||
/** | ||
* Initialize clients storage | ||
*/ | ||
$this->clients = new \SplObjectStorage; | ||
|
||
/** | ||
* Clean database from old connections | ||
* (e.g. connections that were not removed from database because of a crash or a bug) | ||
*/ | ||
try { | ||
$this->cleanWsConnections(); | ||
} catch (Exception $e) { | ||
$this->log('Error while cleaning database from old connections: ' . $e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Return all websocket clients | ||
*/ | ||
public function getClients() | ||
{ | ||
return $this->clients; | ||
} | ||
|
||
/** | ||
* On websocket connection open | ||
*/ | ||
public function onOpen(ConnectionInterface $conn) | ||
{ | ||
$this->clients->attach($conn); | ||
$this->log('[connection #' . $conn->resourceId . '] New connection!'); | ||
|
||
/** | ||
* Adding connection Id to database | ||
*/ | ||
try { | ||
$this->newWsConnection($conn->resourceId); | ||
} catch (Exception $e) { | ||
$this->log('[connection #' . $conn->resourceId . '] Error while adding connection to database: ' . $e->getMessage()); | ||
|
||
/** | ||
* Send a message to the client to inform that the connection is not allowed, and close it | ||
* TODO: Add an error Id to the message | ||
*/ | ||
$conn->send(json_encode(array('error' => "You've been connected but an error occured on the server side. Please try again later."))); | ||
$conn->close(); | ||
} | ||
} | ||
|
||
/** | ||
* On websocket message received | ||
*/ | ||
public function onMessage(ConnectionInterface $conn, $message) | ||
{ | ||
/** | ||
* Decode JSON message | ||
*/ | ||
try { | ||
$message = json_decode($message, true); | ||
} catch (Exception $e) { | ||
$this->log('[connection #' . $conn->resourceId . '] Error while decoding message: ' . $e->getMessage()); | ||
return; | ||
} | ||
|
||
/** | ||
* If the client is sending its connection type | ||
*/ | ||
if (!empty($message['connection-type'])) { | ||
// Connection type must be either 'browser-client' or 'android-client' | ||
if (!in_array($message['connection-type'], array('browser-client', 'android-client'))) { | ||
$this->log('[connection #' . $conn->resourceId . '] Invalid connection type: ' . $message['connection-type']); | ||
|
||
// Close connection | ||
$conn->close(); | ||
} | ||
|
||
// Set connection type in database | ||
$this->setWsConnectionType($conn->resourceId, $message['connection-type']); | ||
} | ||
} | ||
|
||
/** | ||
* On websocket connection close | ||
*/ | ||
public function onClose(ConnectionInterface $conn) | ||
{ | ||
$this->clients->detach($conn); | ||
$this->log('[connection #' . $conn->resourceId . '] Connection closed'); | ||
|
||
/** | ||
* Removing connection Id from database | ||
*/ | ||
try { | ||
$this->deleteWsConnection($conn->resourceId); | ||
} catch (Exception $e) { | ||
$this->log('[connection #' . $conn->resourceId . '] Error while removing connection from database: ' . $e->getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* On websocket connection error | ||
*/ | ||
public function onError(ConnectionInterface $conn, \Exception $e) | ||
{ | ||
$this->log('[connection #' . $conn->resourceId . '] An error occured with connection: ' . $e->getMessage()); | ||
$conn->close(); | ||
} | ||
} |
Oops, something went wrong.