Websocket szerver + kliens & automata frissítés
1 files modified
4 files added
| | |
| | | {% 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 %} |
New file |
| | |
| | | { |
| | | "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" |
| | | } |
| | | } |
New file |
| | |
| | | <?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); |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | <?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"; |
| | | } |
| | | } |
New file |
| | |
| | | <?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(); |