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
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->commands([
@ -17,14 +14,12 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider
]);
}
/**
* Register the application services.
*/
public function register()
{
$this->app->singleton('websockets.router', function() {
return new Router();
});
$this->app->singleton(ChannelManager::class, function() {
return new ChannelManager();
});

View File

@ -19,13 +19,7 @@ class Router
$this->routes = new RouteCollection;
}
/**
* Add a new WebSocket route.
*
* @param $uri
* @param $action
*/
public function websocket($uri, $action)
public function websocket(string $uri, $action)
{
if (!is_subclass_of($action, WebSocketController::class)) {
throw InvalidWebSocketController::withController($action);
@ -34,48 +28,44 @@ class Router
$this->get($uri, $action);
}
public function get($uri, $action)
public function get(string $uri, $action)
{
$this->addRoute('GET', $uri, $action);
}
public function post($uri, $action)
public function post(string $uri, $action)
{
$this->addRoute('POST', $uri, $action);
}
public function put($uri, $action)
public function put(string $uri, $action)
{
$this->addRoute('PUT', $uri, $action);
}
public function patch($uri, $action)
public function patch(string $uri, $action)
{
$this->addRoute('PATCH', $uri, $action);
}
public function delete($uri, $action)
public function delete(string $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));
}
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]);
}
/**
* Register the required Laravel Echo routes
*/
public function echo()
{
//TODO: add orgin checker middleware
//TODO: add origin checker middleware
$this->get('/app/{appId}', LaravelEcho\WebSocket\EchoServer::class);
// 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
* @return WsServer|HttpServerInterface
*/

View File

@ -2,47 +2,28 @@
namespace BeyondCode\LaravelWebSockets;
use Exception;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;
class WebSocketController implements MessageComponentInterface
{
/**
* 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)
function onOpen(ConnectionInterface $connection)
{
dump("Client connected");
}
/**
* 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)
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
{
//
}
/**
* 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)
{
//
}
}