'Android One to One Messaging App Using Ratchet Websocket
I want to create a real time chat app like whatsapp for android using php and ratchet websocket. Every tutorial shows how to use it with group chat (as below code) but I want to use it for private messages either. Is there any way to do this for private chat? If yes, how can I do?
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface {
private $clients;
private $users;
public function __construct()
{
$this->clients = array();
$this->users = array();
}
public function onOpen(ConnectionInterface $conn) {
$this->clients[] = $conn;
echo "New Connection";
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client){
if ($client != $from){
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn) {
echo "Connection closed";
}
public function onError(ConnectionInterface $conn, \Exception $e) {
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
