This commit is contained in:
freek 2018-11-22 00:25:24 +01:00
parent 140d6d9951
commit 7badd6d2fc
3 changed files with 18 additions and 55 deletions

View File

@ -7,9 +7,6 @@ use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
class LaravelWebSocketsServiceProvider extends ServiceProvider class LaravelWebSocketsServiceProvider extends ServiceProvider
{ {
/**
* Bootstrap the application services.
*/
public function boot() public function boot()
{ {
$this->commands([ $this->commands([
@ -17,14 +14,12 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider
]); ]);
} }
/**
* Register the application services.
*/
public function register() public function register()
{ {
$this->app->singleton('websockets.router', function() { $this->app->singleton('websockets.router', function() {
return new Router(); return new Router();
}); });
$this->app->singleton(ChannelManager::class, function() { $this->app->singleton(ChannelManager::class, function() {
return new ChannelManager(); return new ChannelManager();
}); });

View File

@ -19,13 +19,7 @@ class Router
$this->routes = new RouteCollection; $this->routes = new RouteCollection;
} }
/** public function websocket(string $uri, $action)
* Add a new WebSocket route.
*
* @param $uri
* @param $action
*/
public function websocket($uri, $action)
{ {
if (!is_subclass_of($action, WebSocketController::class)) { if (!is_subclass_of($action, WebSocketController::class)) {
throw InvalidWebSocketController::withController($action); throw InvalidWebSocketController::withController($action);
@ -34,48 +28,44 @@ class Router
$this->get($uri, $action); $this->get($uri, $action);
} }
public function get($uri, $action) public function get(string $uri, $action)
{ {
$this->addRoute('GET', $uri, $action); $this->addRoute('GET', $uri, $action);
} }
public function post($uri, $action) public function post(string $uri, $action)
{ {
$this->addRoute('POST', $uri, $action); $this->addRoute('POST', $uri, $action);
} }
public function put($uri, $action) public function put(string $uri, $action)
{ {
$this->addRoute('PUT', $uri, $action); $this->addRoute('PUT', $uri, $action);
} }
public function patch($uri, $action) public function patch(string $uri, $action)
{ {
$this->addRoute('PATCH', $uri, $action); $this->addRoute('PATCH', $uri, $action);
} }
public function delete($uri, $action) public function delete(string $uri, $action)
{ {
$this->addRoute('DELETE', $uri, $action); $this->addRoute('DELETE', $uri, $action);
} }
public function addRoute($method, $uri, $action) public function addRoute(string $method, string $uri, $action)
{ {
$this->routes->add($uri, $this->getRoute($method, $uri, $action)); $this->routes->add($uri, $this->getRoute($method, $uri, $action));
} }
protected function getRoute($method, $uri, $action): Route protected function getRoute(string $method, string $uri, $action): Route
{ {
return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], [$method]); return new Route($uri, ['_controller' => $this->wrapAction($action)], [], [], null, [], [$method]);
} }
/**
* Register the required Laravel Echo routes
*/
public function echo() public function echo()
{ {
//TODO: add orgin checker middleware //TODO: add origin checker middleware
$this->get('/app/{appId}', LaravelEcho\WebSocket\EchoServer::class); $this->get('/app/{appId}', LaravelEcho\WebSocket\EchoServer::class);
// TODO: fleshen out http API // TODO: fleshen out http API
@ -88,9 +78,6 @@ class Router
} }
/** /**
* Wrap WebSocket controllers with Ratchets WsServer.
* If the action is not a WebSocketController, wrap it with our HttpServerInstance
*
* @param $action * @param $action
* @return WsServer|HttpServerInterface * @return WsServer|HttpServerInterface
*/ */

View File

@ -2,47 +2,28 @@
namespace BeyondCode\LaravelWebSockets; namespace BeyondCode\LaravelWebSockets;
use Exception;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface; use Ratchet\WebSocket\MessageComponentInterface;
class WebSocketController implements MessageComponentInterface class WebSocketController implements MessageComponentInterface
{ {
function onOpen(ConnectionInterface $connection)
/**
* When a new connection is opened it will be passed to this method
* @param ConnectionInterface $conn The socket/connection that just connected to your application
* @throws \Exception
*/
function onOpen(ConnectionInterface $conn)
{ {
dump("Client connected"); dump("Client connected");
} }
/** public function onMessage(ConnectionInterface $connection, MessageInterface $message)
* 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)
{
//
}
public function onMessage(ConnectionInterface $conn, MessageInterface $msg) function onClose(ConnectionInterface $connection)
{
}
function onError(ConnectionInterface $connection, Exception $exception)
{ {
//
} }
} }