clients = new \SplObjectStorage; } protected function handleLaravelRequest(ConnectionInterface $con, $route, $data = null) { /** * @var \Ratchet\WebSocket\Version\RFC6455\Connection $con * @var \Guzzle\Http\Message\Request $wsrequest * @var \Illuminate\Http\Response $response */ $params = [ 'connection' => $con, 'other_clients' => [], ]; if ($data !== null) { if (is_string($data)) { $params = ['data' => json_decode($data)]; } else { $params = ['data' => $data]; } } foreach ($this->clients as $client) { if ($con != $client) { $params['other_clients'][] = $client; } else { $params['current_client'] = $client; } } $wsrequest = $con->WebSocket->request; $app = require __DIR__.'/bootstrap/app.php'; $kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); $response = $kernel->handle( $request = Illuminate\Http\Request::create($route, 'GET', $params, $wsrequest->getCookies()) ); //var_dump(Auth::id()); $controllerResult = $response->getContent(); $kernel->terminate($request, $response); return json_encode($controllerResult); } public function onOpen(ConnectionInterface $con) { $this->clients->attach($con); $this->handleLaravelRequest($con, '/websocket/open'); } public function onMessage(ConnectionInterface $con, $msg) { $this->handleLaravelRequest($con, '/websocket/message', $msg); } public function onClose(ConnectionInterface $con) { $this->handleLaravelRequest($con, '/websocket/close'); $this->clients->detach($con); } public function onError(ConnectionInterface $con, \Exception $e) { $this->handleLaravelRequest($con, '/websocket/error'); echo 'Error: ' . $e->getMessage() . PHP_EOL; $con->close(); } } // Run the server application through the WebSocket protocol on port 8080 $app = new Ratchet\App('localhost', 8080); $app->route('/echo', new WebSocketLaravelServer); $app->run();