использовать Memcached для трещотки - PullRequest
0 голосов
/ 22 ноября 2018

Я ищу в Интернете пример использования Memcahced в rachet, но ничего не нашел.

Я хочу использовать Memcached в rachet для sessoinProvider, у меня есть ChatShell, который я запускаю с помощью команды, Chat, которое является моим приложением.

Я пытался реализовать sessoinProvider, но возникла ошибка:

Аргумент 1 передан в Ratchet \ Session \ SessionProvider :: __ construct () mustреализовать интерфейс Ratchet \ Http \ HttpServerInterface, экземпляр данного App \ Http \ Controllers \ WebSockets \ Chat, названный в /media/e/www/e-learning/app/Console/Commands/ChatShell.php в строке 54

Мой ChatShell.php:

namespace App\Console\Commands;

use Ratchet\Session\SessionProvider;
use Illuminate\Console\Command;
use Ratchet\Server\IoServer;
use App\Http\Controllers\WebSockets\Chat;
use Ratchet\WebSocket\WsServer;
use Ratchet\Http\HttpServer;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;

class ChatShell extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:chat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $memcached = new \Memcached();


        $server = IoServer::factory(
            new HttpServer(
                new WsServer(
                    new SessionProvider(
                        new Chat(),
                        new Handler\MemcachedSessionHandler($memcached)
                    )
                )
            ),
            6502
        );

        $server->run();
    }
}

и мой Chat.php

    <?php

namespace App\Http\Controllers\WebSockets;

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface {

    /** @var Client[] $clients */
    protected $clients;

    public function __construct() {
        $this->clients = [];}

    /**
     * @param ConnectionInterface $conn
     */
    public function onOpen(ConnectionInterface $conn) {

        $this->clients[$conn->resourceId] = new Client();
        $this->clients[$conn->resourceId]->conn = $conn;


        echo "New connection! ({$conn->resourceId}) {$this->clients[$conn->resourceId]->name}\n";
    }

    /**
     * @param ConnectionInterface $from
     * @param string $msg
     */
    public function onMessage( ConnectionInterface $from, $msg) {
    //send message
    }

    public function onClose(ConnectionInterface $conn) {
        // The connection is closed, remove it, as we can no longer send it messages
        unset($this->clients[$conn->resourceId]);

        echo "Connection {$conn->resourceId} has disconnected\n";
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        echo "An error has occurred: {$e->getMessage()}\n";

        $conn->close();
    }

    /**
     * @param array $msg
     */
    function sendToAllUser($msg)
    {
        if(is_array($msg))
        {
            foreach ($this->clients as $client) {
                $client->conn->send(json_encode($msg));
            }
        }
        else
        {
            foreach ($this->clients as $client) {
                $client->conn->send($msg);
            }
        }

    }

}

1- что я делаю не так?

2-как использовать Memchached в этом случае, хотя я знаю, что ошибка вызвана sessionProvider, но я не знаю, как использовать Memcached здесь.

...