Показать сообщение отдельно
  #13 (permalink)  
Старый 23.08.2019, 15:11
Интересующийся
Отправить личное сообщение для teplov Посмотреть профиль Найти все сообщения от teplov
 
Регистрация: 23.08.2019
Сообщений: 13

вот обработчик
<?php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;


require_once dirname(__FILE__) . '/../../configuration.php';
require_once dirname(__FILE__) . '/Objects/ChatObj.php';

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) {
        $p = \Plugin::loadPlugin("Chat");
        $object = json_decode($msg);
        $canSendMessage = $p->canSendMessage($object->userId);
        if(empty($canSendMessage)){
            echo "Cant Send message\n";
            return false;
        }
        //var_dump($msg);
        echo "Saving message\n";
        $lc = new \ChatObj(0);
        $lc->setStatus('a');
        $lc->setText($object->text);
        $lc->setUsers_id($object->userId);
        $lc->save();
        
        $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($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $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();
    }    
    
    public function getTags() {
        return array('free', 'chat');
    }
}
Ответить с цитированием