From 18a67a86de42e5712af370cc7b85a7831f3757f0 Mon Sep 17 00:00:00 2001 From: GarryOne Date: Sat, 25 Jul 2015 03:20:05 +0300 Subject: [PATCH] Added support for online/offline status of users Every time an user connects or disconnects, he sends a message to all others so that others can mark his status as offline/online. --- src/BrainSocket/BrainSocketEventListener.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/BrainSocket/BrainSocketEventListener.php b/src/BrainSocket/BrainSocketEventListener.php index 46c28b5..3b53ec1 100644 --- a/src/BrainSocket/BrainSocketEventListener.php +++ b/src/BrainSocket/BrainSocketEventListener.php @@ -17,6 +17,10 @@ public function __construct(BrainSocketResponseInterface $response) { public function onOpen(ConnectionInterface $conn) { echo "Connection Established! \n"; $this->clients->attach($conn); + + foreach ($this->clients as $client) { ++ $client->send($this->response->make('someone_connected')); ++ } } public function onMessage(ConnectionInterface $from, $msg) { @@ -33,10 +37,18 @@ public function onMessage(ConnectionInterface $from, $msg) { public function onClose(ConnectionInterface $conn) { $this->clients->detach($conn); echo "Connection {$conn->resourceId} has disconnected\n"; + ++ foreach ($this->clients as $client) { ++ $client->send($this->response->make('someone_disconnected')); ++ } ++ ++ $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(); } -} \ No newline at end of file +}