This commit is contained in:
Marcel Pociot 2018-11-20 11:32:56 +01:00
parent 0fa9213133
commit 6796fb4062
9 changed files with 156 additions and 32 deletions

View File

@ -1,4 +1,4 @@
# Laravel Websockets 🚀 # Laravel WebSockets 🚀
[![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-websockets.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-websockets) [![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/laravel-websockets.svg?style=flat-square)](https://packagist.org/packages/beyondcode/laravel-websockets)
[![Build Status](https://img.shields.io/travis/beyondcode/laravel-websockets/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-websockets) [![Build Status](https://img.shields.io/travis/beyondcode/laravel-websockets/master.svg?style=flat-square)](https://travis-ci.org/beyondcode/laravel-websockets)
@ -18,7 +18,7 @@ composer require beyondcode/laravel-websockets
## Usage ## Usage
``` php ``` php
$skeleton = new BeyondCode\LaravelWebsockets(); $skeleton = new BeyondCode\LaravelWebSockets();
echo $skeleton->echoPhrase('Hello, BeyondCode!'); echo $skeleton->echoPhrase('Hello, BeyondCode!');
``` ```

View File

@ -23,7 +23,10 @@
], ],
"require": { "require": {
"php": "^7.1", "php": "^7.1",
"illuminate/http": "5.6.*|5.7.*" "cboden/ratchet": "^0.4.1",
"illuminate/support": "5.6.*|5.7.*",
"illuminate/http": "5.6.*|5.7.*",
"illuminate/routing": "5.6.*|5.7.*"
}, },
"require-dev": { "require-dev": {
"larapack/dd": "^1.0", "larapack/dd": "^1.0",
@ -31,12 +34,12 @@
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"BeyondCode\\LaravelWebsockets\\": "src" "BeyondCode\\LaravelWebSockets\\": "src"
} }
}, },
"autoload-dev": { "autoload-dev": {
"psr-4": { "psr-4": {
"BeyondCode\\LaravelWebsockets\\Tests\\": "tests" "BeyondCode\\LaravelWebSockets\\Tests\\": "tests"
} }
}, },
"scripts": { "scripts": {
@ -50,8 +53,11 @@
"extra": { "extra": {
"laravel": { "laravel": {
"providers": [ "providers": [
"BeyondCode\\LaravelWebsockets\\LaravelWebsocketsServiceProvider" "BeyondCode\\LaravelWebSockets\\LaravelWebSocketsServiceProvider"
] ],
"aliases": {
"WebSocketRouter": "BeyondCode\\LaravelWebSockets\\Facades\\WebSocketRouter"
}
} }
} }
} }

View File

@ -0,0 +1,11 @@
<?php
namespace BeyondCode\LaravelWebSockets\Exceptions;
class InvalidWebSocketController extends \Exception
{
public static function withController($controller)
{
return new static('Invalid WebSocket Controller provided. Expected instance of WebSocketController, but received '.$controller);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace BeyondCode\LaravelWebSockets\Facades;
use Illuminate\Support\Facades\Facade;
/**
* @see \BeyondCode\LaravelWebSockets\Router
*/
class WebSocketRouter extends Facade
{
/**
* Get the registered name of the component.
*
* @return string
*/
protected static function getFacadeAccessor()
{
return 'websockets.router';
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace BeyondCode\LaravelWebSockets;
use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router as LaravelRouter;
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
class LaravelWebSocketsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
LaravelRouter::macro('websocket', function($uri, $action) {
WebSocketRouter::addRoute($uri, $action);
});
}
/**
* Register the application services.
*/
public function register()
{
$this->app->singleton('websockets.router', function() {
return new Router();
});
}
}

View File

@ -1,24 +0,0 @@
<?php
namespace BeyondCode\LaravelWebsockets;
use Illuminate\Support\ServiceProvider;
class LaravelWebsocketsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
}
/**
* Register the application services.
*/
public function register()
{
}
}

32
src/Router.php Normal file
View File

@ -0,0 +1,32 @@
<?php
namespace BeyondCode\LaravelWebSockets;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
class Router
{
/** @var RouteCollection */
protected $routes;
public function __construct()
{
$this->routes = new RouteCollection;
}
public function addRoute($uri, $action)
{
if (! is_subclass_of($action, WebSocketController::class)) {
throw InvalidWebSocketController::withController($action);
}
$this->routes->add($uri, $this->getRoute($uri, $action));
}
protected function getRoute($uri, $action) : Route
{
return new Route($uri, ['_controller' => app($action)], [], [], null, [], ['GET']);
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace BeyondCode\LaravelWebSockets;
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)
{
//
}
/**
* 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)
{
//
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace BeyondCode\LaravelWebsockets\Tests; namespace BeyondCode\LaravelWebSockets\Tests;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;