Fibinger Ádám
2020-06-04 39aea3acd86d676de0923016c2b0eecd79d9a7a9
Websocket szerver + kliens & automata frissítés
1 files modified
4 files added
180 ■■■■■ changed files
templates/overlays/stripe.twig 21 ●●●●● patch | view | raw | blame | history
ws-com/composer.json 29 ●●●●● patch | view | raw | blame | history
ws-com/src/Chat.php 48 ●●●●● patch | view | raw | blame | history
ws-com/src/Pusher.php 53 ●●●●● patch | view | raw | blame | history
ws-com/websocket.php 29 ●●●●● patch | view | raw | blame | history
templates/overlays/stripe.twig
@@ -71,12 +71,29 @@
            {% endblock %}
            {% block info_right %}
            <span class="info right">
                {% if stripe.cup.number%}
                <span class="info right">
                {% if stripe.cup.number %}
                    <img class="wargasz-logo" src="/assets/WARGASZ_color.png"/>
                {% endif %}
            </span>
            {% endblock %}
        </div>
    {% endblock %}
    <script type="text/javascript">
        // Create WebSocket connection.
        const socket = new WebSocket('ws://esl.unr.hu/generator/ws-com/');
        // Connection opened
        socket.addEventListener('open', function (event) {
            console.log("Sending server: " + "Hello Bitch!");
            socket.send('Hello Bitch!');
        });
        // Listen for messages
        socket.addEventListener('message', function (event) {
            console.log('Message from server ', event.data);
            if (event.data === "update") {
                location.reload();
            }
        });
    </script>
{% endblock %}
ws-com/composer.json
New file
@@ -0,0 +1,29 @@
{
  "repositories": [
    {
      "type": "package",
      "package": {
        "name": "laravie/predis-async",
        "version": "1",
        "type": "library",
        "source": {
          "url": "https://github.com/laravie/predis-async",
          "type": "git",
          "reference": "master"
        }
      }
    }
  ],
  "autoload": {
    "psr-4": {
      "WS\\": "src",
      "Predis\\Async\\": "vendor/laravie/predis-async/src/"
    }
  },
  "require": {
    "cboden/ratchet": "*",
    "laravie/predis-async": "*",
    "predis/predis": "*",
    "clue/redis-protocol": "^0.3.1"
  }
}
ws-com/src/Chat.php
New file
@@ -0,0 +1,48 @@
<?php
namespace WS;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
    protected $clients;
    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
    }
    public function onOpen(ConnectionInterface $conn)
    {
        $this->clients->attach($conn);
        $conn->send("Hello overlay client!");
    }
    public function onMessage(ConnectionInterface $conn, $msg)
    {
        $conn->send("Never talk to me like that again!");
    }
    public function onClose(ConnectionInterface $conn)
    {
        $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();
    }
    public function sendToClients($message)
    {
        foreach ($this->clients as $client)
        {
            $client->send($message);
        }
    }
}
ws-com/src/Pusher.php
New file
@@ -0,0 +1,53 @@
<?php
namespace WS;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
class Pusher implements MessageComponentInterface
{
    protected $clients;
    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
    }
    public function broadcast($msg)
    {
        foreach ($this->clients as $client)
        {
            // The sender is not the receiver, send to each client connected
            $client->send($msg);
        }
    }
    public function onOpen(ConnectionInterface $conn)
    {
        echo __METHOD__ . "\n";
        // Store the new connection to send messages to later
        $this->clients->attach($conn);
        echo "New connection! ({$conn->resourceId})\n";
    }
    public function onClose(ConnectionInterface $conn)
    {
        echo __METHOD__ . "\n";
        $this->clients->detach($conn);
        echo "Connection {$conn->resourceId} has disconnected\n";
    }
    function onMessage(ConnectionInterface $conn, $msg)
    {
        echo __METHOD__ . "\n";
        echo $msg . PHP_EOL;
        $conn->send("https://www.youtube.com/watch?v=TR3Vdo5etCQ");
    }
    public function onError(ConnectionInterface $conn, \Exception $e)
    {
        echo __METHOD__ . "\n";
    }
}
ws-com/websocket.php
New file
@@ -0,0 +1,29 @@
<?php
require './vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$pusher = new WS\Pusher;
$client = new Predis\Async\Client('tcp://127.0.0.1:6379', $loop);
$client->connect(function ($client) use ($pusher) {
    /** @var Predis\Async\Client $client */
    $client->pubSubLoop('overlay', function ($event, $pubsub) use ($pusher) {
        $pusher->broadcast($event->payload);
        var_dump($event->payload);
    });
});
if (!$client->isConnected())
{
    die("Fatal error: TCP connection to redis-server is closed\n");
}
$webSock = new React\Socket\Server('127.0.0.1:8000', $loop);
$webServer = new Ratchet\Server\IoServer(
    new \Ratchet\Http\HttpServer(
        new \Ratchet\WebSocket\WsServer(
            $pusher
        )
    ), $webSock);
$loop->run();