summaryrefslogtreecommitdiff
path: root/src/Ratchet
diff options
context:
space:
mode:
authorFbenas <philbeansburton@gmail.com>2017-03-12 23:57:42 +0000
committerFbenas <philbeansburton@gmail.com>2017-03-12 23:57:42 +0000
commit1eee94d2604fb43b5a4a21c281c72ad356906a88 (patch)
tree23c095b5db60c48dae443356392f5a5eb8a9c9f2 /src/Ratchet
parentedfd095021ee5d89f53da4fd78d7dea7346d7617 (diff)
Added identify form and Ratchet websockets test stuff
Diffstat (limited to 'src/Ratchet')
-rw-r--r--src/Ratchet/Chat.php59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/Ratchet/Chat.php b/src/Ratchet/Chat.php
new file mode 100644
index 0000000..3410cf4
--- /dev/null
+++ b/src/Ratchet/Chat.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace App\Ratchet;
+
+use Ratchet\MessageComponentInterface;
+use Ratchet\ConnectionInterface;
+use SplObjectStorage;
+
+class Chat implements MessageComponentInterface
+{
+ protected $clients;
+
+ 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)
+ {
+ $numRecv = count($this->clients) - 1;
+ echo sprintf(
+ 'Connection %d sending message "%s" to %d other connection%s' . "\n",
+ $from->resourceId,
+ $msg,
+ $numRecv,
+ $numRecv == 1 ? '' : 's'
+ );
+
+ foreach ($this->clients as $client) {
+ if ($from !== $client) {
+ // The sender is not the receiver, send to each client connected
+ $client->send($msg);
+ }
+ }
+ }
+
+ 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();
+ }
+}