From fc9401f04a3aca5abb22f87ebc210de8afe11d32 Mon Sep 17 00:00:00 2001 From: marvin-borner@live.com Date: Tue, 10 Apr 2018 21:50:16 +0200 Subject: Initial Commit --- assets/php/src/ChatProcessor.php | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 assets/php/src/ChatProcessor.php (limited to 'assets/php/src/ChatProcessor.php') diff --git a/assets/php/src/ChatProcessor.php b/assets/php/src/ChatProcessor.php new file mode 100644 index 0000000..c43f968 --- /dev/null +++ b/assets/php/src/ChatProcessor.php @@ -0,0 +1,93 @@ +clients = new \SplObjectStorage; + $this->subscriptions = []; + $this->users = []; + $this->connectedUsersNames = []; + } + + public function onOpen(ConnectionInterface $conn) { + $generator = new \Nubs\RandomNameGenerator\Alliteration(); + $this->clients->attach($conn); + $this->users[$conn->resourceId] = $conn; + $this->connectedUsersNames[$conn->resourceId] = $generator->getName(); + + 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) { + $client->send("You - " . $msg); + } else { + $client->send("" . $from->resourceId . " - " . $msg); + } + } + } + */ + + public function onMessage(ConnectionInterface $conn, MessageInterface $msg) { + $data = json_decode($msg); + switch ($data->command) { + case "subscribe": + $this->subscriptions[$conn->resourceId] = $data->channel; + foreach ($this->subscriptions as $id => $channel) { + if ($this->subscriptions[$conn->resourceId] == $channel) { + if ($id === $conn->resourceId) { + $this->users[$id]->send("You (" . $this->connectedUsersNames[$conn->resourceId] . ") joined this group."); + } else { + $this->users[$id]->send("User (" . $this->connectedUsersNames[$conn->resourceId] . ") joined this group."); + } + } + } + break; + case "message": + if (isset($this->subscriptions[$conn->resourceId])) { + $target = $this->subscriptions[$conn->resourceId]; + foreach ($this->subscriptions as $id => $channel) { + if ($channel == $target && $id == $conn->resourceId) { + $this->users[$id]->send("You - " . $data->message); + } else if ($channel == $target && $id != $conn->resourceId) { + $this->users[$id]->send("" . $this->connectedUsersNames[$conn->resourceId] . " - " . $data->message); + } + } + } + } + } + + public function onClose(ConnectionInterface $conn) { + $this->clients->detach($conn); + echo "Connection {$conn->resourceId} has disconnected\n"; + foreach ($this->clients as $client) { + $client->send("User " . $this->connectedUsersNames[$conn->resourceId] . " has disconnected"); + } + unset($this->users[$conn->resourceId]); + unset($this->subscriptions[$conn->resourceId]); + unset($this->connectedUsersNames[$conn->resourceId]); + } + + public function onError(ConnectionInterface $conn, \Exception $e) { + echo "An error has occurred: {$e->getMessage()}\n"; + + $conn->close(); + } +} \ No newline at end of file -- cgit v1.2.3