Trigger websocket update after score & image upload
This commit is contained in:
parent
ec07837833
commit
5be4789186
4 changed files with 96 additions and 1 deletions
26
bin/socket_server.php
Normal file
26
bin/socket_server.php
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
chdir(__DIR__);
|
||||||
|
|
||||||
|
// define('APPLICATION_ENV', 'development');
|
||||||
|
|
||||||
|
require_once __DIR__ . "/../bootstrap.php";
|
||||||
|
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
ini_set('display_errors',1);
|
||||||
|
// Show table creation statements
|
||||||
|
// And (re)generate Proxies
|
||||||
|
|
||||||
|
use Ratchet\WebSocket\WsServer;
|
||||||
|
use Ratchet\Http\HttpServer;
|
||||||
|
use Ratchet\Server\IoServer;
|
||||||
|
|
||||||
|
$server = IoServer::factory(
|
||||||
|
new HttpServer(
|
||||||
|
new WsServer(
|
||||||
|
new EmotionHero\Server\Websockets()
|
||||||
|
)
|
||||||
|
),
|
||||||
|
8181
|
||||||
|
);
|
||||||
|
|
||||||
|
$server->run();
|
|
@ -16,7 +16,9 @@
|
||||||
"cnam/security-jwt-service-provider": "2.*",
|
"cnam/security-jwt-service-provider": "2.*",
|
||||||
"jms/serializer": "^1.3",
|
"jms/serializer": "^1.3",
|
||||||
"danielstjules/php-pretty-datetime": "dev-master",
|
"danielstjules/php-pretty-datetime": "dev-master",
|
||||||
"fustundag/tokenbucket": "dev-master"
|
"fustundag/tokenbucket": "dev-master",
|
||||||
|
"cboden/ratchet": "^0.3.5",
|
||||||
|
"textalk/websocket": "^1.2"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
|
|
@ -7,6 +7,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Silex\Api\ControllerProviderInterface;
|
use Silex\Api\ControllerProviderInterface;
|
||||||
use EmotionHero\Models;
|
use EmotionHero\Models;
|
||||||
|
use EmotionHero\Server\Websockets;
|
||||||
|
|
||||||
class ScoreControllerProvider implements ControllerProviderInterface
|
class ScoreControllerProvider implements ControllerProviderInterface
|
||||||
{
|
{
|
||||||
|
@ -140,6 +141,13 @@ class ScoreControllerProvider implements ControllerProviderInterface
|
||||||
$userRepo = $this->_eh->getEm()->getRepository(Models\User::class);
|
$userRepo = $this->_eh->getEm()->getRepository(Models\User::class);
|
||||||
$userRepo->updateTotalForUser($user);
|
$userRepo->updateTotalForUser($user);
|
||||||
|
|
||||||
|
// trigger update of client:
|
||||||
|
try{
|
||||||
|
Websockets::triggerUpdate('game');
|
||||||
|
}catch(\Throwable $e) {
|
||||||
|
// nothing
|
||||||
|
}
|
||||||
|
|
||||||
return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200);
|
return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -205,6 +213,14 @@ class ScoreControllerProvider implements ControllerProviderInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$em->flush();
|
$em->flush();
|
||||||
|
|
||||||
|
|
||||||
|
// trigger update of client:
|
||||||
|
try{
|
||||||
|
Websockets::triggerUpdate('images');
|
||||||
|
}catch(\Throwable $e) {
|
||||||
|
// nothing
|
||||||
|
}
|
||||||
|
|
||||||
return new JsonResponse(['success' => true, 'message' => "images saved."], 200);
|
return new JsonResponse(['success' => true, 'message' => "images saved."], 200);
|
||||||
});
|
});
|
||||||
|
|
51
src/Server/Websockets.php
Normal file
51
src/Server/Websockets.php
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
namespace EmotionHero\Server;
|
||||||
|
use WebSocket\Client;
|
||||||
|
|
||||||
|
class Websockets
|
||||||
|
{
|
||||||
|
protected $clients;
|
||||||
|
|
||||||
|
public static function triggerUpdate(string $type) {
|
||||||
|
$client = new Client("ws://localhost:8181/");
|
||||||
|
$client->send(json_encode(['action'=>'update', 'method'=>$type]));
|
||||||
|
$client->receive();
|
||||||
|
unset($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->clients = new \SplObjectStorage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onOpen(ConnectionInterface $conn) {
|
||||||
|
// Store the new connection to send messages to later
|
||||||
|
$this->clients->attach($conn);
|
||||||
|
|
||||||
|
echo "New connection! ({$conn->resourceId})\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onMessage(ConnectionInterface $from, $msg) {
|
||||||
|
echo sprintf('Connection %d sending message "%s"' . "\n"
|
||||||
|
, $from->resourceId, $msg);
|
||||||
|
|
||||||
|
foreach ($this->clients as $client) {
|
||||||
|
if ($from !== $client) {
|
||||||
|
// The sender is not the receiver, send to each client connected
|
||||||
|
$client->send("update");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onClose(ConnectionInterface $conn) {
|
||||||
|
// The connection is closed, remove it, as we can no longer send it messages
|
||||||
|
$this->clients->detach($conn);
|
||||||
|
|
||||||
|
echo "Connection {$conn->resourceId} has disconnected\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onError(ConnectionInterface $conn, \Exception $e) {
|
||||||
|
echo "An error has occurred: {$e->getMessage()}\n";
|
||||||
|
|
||||||
|
$conn->close();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue