This commit is contained in:
Marcel Pociot 2018-11-20 14:50:37 +01:00
parent a09356e40c
commit bf4df423b2
4 changed files with 96 additions and 7 deletions

View File

@ -24,10 +24,10 @@
"require": { "require": {
"php": "^7.1", "php": "^7.1",
"cboden/ratchet": "^0.4.1", "cboden/ratchet": "^0.4.1",
"illuminate/support": "5.6.*|5.7.*",
"illuminate/console": "5.6.*|5.7.*", "illuminate/console": "5.6.*|5.7.*",
"illuminate/http": "5.6.*|5.7.*", "illuminate/http": "5.6.*|5.7.*",
"illuminate/routing": "5.6.*|5.7.*" "illuminate/routing": "5.6.*|5.7.*",
"illuminate/support": "5.6.*|5.7.*"
}, },
"require-dev": { "require-dev": {
"larapack/dd": "^1.0", "larapack/dd": "^1.0",

View File

@ -0,0 +1,60 @@
<?php
namespace BeyondCode\LaravelWebsockets\LaravelEcho\Http\Controllers;
use Ratchet\ConnectionInterface;
use Illuminate\Http\JsonResponse;
use Ratchet\Http\HttpServerInterface;
use Psr\Http\Message\RequestInterface;
abstract class EchoController implements HttpServerInterface
{
/**
* This is called before or after a socket is closed (depends on how it's closed). SendMessage to $conn will not result in an error if it has already been closed.
* @param ConnectionInterface $conn The socket/connection that is closing/closed
* @throws \Exception
*/
function onClose(ConnectionInterface $conn)
{
//
}
/**
* If there is an error with one of the sockets, or somewhere in the application where an Exception is thrown,
* the Exception is sent back down the stack, handled by the Server and bubbled back up the application through this method
* @param ConnectionInterface $conn
* @param \Exception $e
* @throws \Exception
*/
function onError(ConnectionInterface $conn, \Exception $e)
{
//
}
/**
* @param \Ratchet\ConnectionInterface $conn
* @param \Psr\Http\Message\RequestInterface $request null is default because PHP won't let me overload; don't pass null!!!
* @throws \UnexpectedValueException if a RequestInterface is not passed
*/
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
$response = $this($request);
$conn->send(JsonResponse::create($response)->send());
$conn->close();
}
/**
* Triggered when a client sends data through the socket
* @param \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application
* @param string $msg The message received
* @throws \Exception
*/
function onMessage(ConnectionInterface $from, $msg)
{
//
}
abstract public function __invoke($request);
}

View File

@ -0,0 +1,14 @@
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
class StatusController extends EchoController
{
public function __invoke($request)
{
return [
'subscription_count' => 10
];
}
}

View File

@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets;
use Ratchet\WebSocket\WsServer; use Ratchet\WebSocket\WsServer;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Ratchet\Http\HttpServerInterface;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController; use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
@ -39,19 +40,33 @@ class Router
protected function getRoute($uri, $action): Route protected function getRoute($uri, $action): Route
{ {
return new Route($uri, ['_controller' => $this->wrapController($action)], [], [], null, [], ['GET']); return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], ['GET']);
}
/**
* Register the required Laravel Echo routes
*/
public function echo()
{
//$this->addRoute('/', EchoWebsocketServer::class);
$this->addRoute('/apps/{appId}/status', LaravelEcho\Http\Controllers\StatusController::class);
//$this->addRoute('/apps/{appId}/channels', 'ChannelController@index');
} }
/** /**
* Wrap WebSocket controllers with Ratchets WsServer. * Wrap WebSocket controllers with Ratchets WsServer.
* If the action is not a WebSocketController, wrap it with our HttpServerInstance
*
* @param $action
* @return WsServer|HttpServerInterface
*/ */
protected function wrapController($controller) protected function wrapAction($action)
{ {
if (is_subclass_of($controller, WebSocketController::class)) { if (is_subclass_of($action, WebSocketController::class)) {
return new WsServer(app($controller)); return new WsServer(app($action));
} }
return app($controller); return app($action);
} }
public function getRoutes(): RouteCollection public function getRoutes(): RouteCollection