Skip to content

Instantly share code, notes, and snippets.

@harendra21
Created February 25, 2019 10:12
Show Gist options
  • Save harendra21/6e123a7d408ce08a09edeff679c3f643 to your computer and use it in GitHub Desktop.
Save harendra21/6e123a7d408ce08a09edeff679c3f643 to your computer and use it in GitHub Desktop.
<?php
namespace App;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
protected $clients;
private $activeUsers;
private $activeConnections;
public function __construct()
{
$this->clients = new \SplObjectStorage;
$this->activeUsers = [];
$this->activeConnections = [];
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg)
{
$jsonMsg = json_decode($msg);
if ($jsonMsg->type == "login") {
$onlineUsers = [];
$onlineUsers['type'] = "onlineUsers";
$this->activeUsers[$from->resourceId] = $jsonMsg->name;
$onlineUsers['onlineUsers'] = $this->activeUsers;
$this->sendMessageToAll(json_encode($onlineUsers));
} elseif ($jsonMsg->type == "message") {
$this->sendMessageToOthers($from, json_encode($jsonMsg));
}
}
public function sendMessageToOthers($from, $msg)
{
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
}
public function sendMessageToAll($msg)
{
foreach ($this->clients as $client) {
$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);
unset($this->activeUsers[$conn->resourceId]);
$onlineUsers = [];
$onlineUsers['type'] = "onlineUsers";
$onlineUsers['onlineUsers'] = $this->activeUsers;
$this->sendMessageToOthers($conn, json_encode($onlineUsers));
echo "Connection {$conn->resourceId} has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e)
{
echo "An error has occurred: {$e->getMessage()}\n";
$conn->close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment