传递给Ratchet \ Session \ SessionProvider :: __ construct()的参数1必须实现接口Ratchet \ Http \ HttpServerInterface

时间:2018-11-23 11:58:03

标签: php laravel session websocket ratchet

我正在尝试在Ratchet中使用sessionProvider,以下是我的shell脚本:

    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();
    }
}

发生错误,说:

  

传递给Ratchet \ Session \ SessionProvider :: __ construct()的参数1必须实现接口Ratchet \ Http \ HttpServerInterface,App \ Http \ Controllers \ WebSockets \ Chat实例

如何解决此问题!

我的聊天类实现MessageComponentInterface

1 个答案:

答案 0 :(得分:0)

经过长时间的搜索,我找到了解决方案,即使ratchet document说WsServer包装的sessionProvider,sessionProvider应该包装 WsServer。

经线应改为:

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