From ffa7c6f5c007dfc826aef0ca27d23bb69bbeed7e Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Thu, 04 Jun 2020 23:10:08 +0200 Subject: [PATCH] BO pöttyök + BestOf kupa típus --- ws-com/src/Pusher.php | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 53 insertions(+), 0 deletions(-) diff --git a/ws-com/src/Pusher.php b/ws-com/src/Pusher.php new file mode 100644 index 0000000..252fa46 --- /dev/null +++ b/ws-com/src/Pusher.php @@ -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"; + } +} \ No newline at end of file -- Gitblit v1.8.0