wip
This commit is contained in:
parent
6796fb4062
commit
59aad53091
|
|
@ -25,6 +25,7 @@
|
||||||
"php": "^7.1",
|
"php": "^7.1",
|
||||||
"cboden/ratchet": "^0.4.1",
|
"cboden/ratchet": "^0.4.1",
|
||||||
"illuminate/support": "5.6.*|5.7.*",
|
"illuminate/support": "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.*"
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebsockets\Console;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use BeyondCode\LaravelWebSockets\Server\WebSocketServer;
|
||||||
|
|
||||||
|
use React\EventLoop\Factory as LoopFactory;
|
||||||
|
|
||||||
|
class StartWebSocketServer extends Command
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'websocket:start {--port=6001}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Start the Laravel WebSocket Server';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$url = parse_url(config('app.url'));
|
||||||
|
|
||||||
|
$loop = LoopFactory::create();
|
||||||
|
|
||||||
|
$loop->futureTick(function () use ($url) {
|
||||||
|
$this->info('Started the WebSocket server on port '.$this->option('port'));
|
||||||
|
});
|
||||||
|
|
||||||
|
$server = new WebsocketServer($url['host'], $this->option('port'), '0.0.0.0', $loop);
|
||||||
|
$server->run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,6 +16,10 @@ class LaravelWebSocketsServiceProvider extends ServiceProvider
|
||||||
LaravelRouter::macro('websocket', function($uri, $action) {
|
LaravelRouter::macro('websocket', function($uri, $action) {
|
||||||
WebSocketRouter::addRoute($uri, $action);
|
WebSocketRouter::addRoute($uri, $action);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->commands([
|
||||||
|
Console\StartWebSocketServer::class,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace BeyondCode\LaravelWebSockets;
|
namespace BeyondCode\LaravelWebSockets;
|
||||||
|
|
||||||
|
use Ratchet\WebSocket\WsServer;
|
||||||
use Symfony\Component\Routing\Route;
|
use Symfony\Component\Routing\Route;
|
||||||
use Symfony\Component\Routing\RouteCollection;
|
use Symfony\Component\Routing\RouteCollection;
|
||||||
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
|
use BeyondCode\LaravelWebSockets\Exceptions\InvalidWebSocketController;
|
||||||
|
|
@ -18,15 +19,32 @@ class Router
|
||||||
|
|
||||||
public function addRoute($uri, $action)
|
public function addRoute($uri, $action)
|
||||||
{
|
{
|
||||||
if (! is_subclass_of($action, WebSocketController::class)) {
|
if (!is_subclass_of($action, WebSocketController::class)) {
|
||||||
throw InvalidWebSocketController::withController($action);
|
throw InvalidWebSocketController::withController($action);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->routes->add($uri, $this->getRoute($uri, $action));
|
$this->routes->add($uri, $this->getRoute($uri, $action));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getRoute($uri, $action) : Route
|
protected function getRoute($uri, $action): Route
|
||||||
{
|
{
|
||||||
return new Route($uri, ['_controller' => app($action)], [], [], null, [], ['GET']);
|
return new Route($uri, ['_controller' => $this->wrapController($action)], [], [], null, [], ['GET']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrap WebSocket controllers with Ratchets WsServer.
|
||||||
|
*/
|
||||||
|
protected function wrapController($controller)
|
||||||
|
{
|
||||||
|
if (is_subclass_of($controller, WebSocketController::class)) {
|
||||||
|
return new WsServer(app($controller));
|
||||||
|
}
|
||||||
|
|
||||||
|
return app($controller);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRoutes(): RouteCollection
|
||||||
|
{
|
||||||
|
return $this->routes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,89 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace BeyondCode\LaravelWebsockets\Server;
|
||||||
|
|
||||||
|
use Ratchet\Http\Router;
|
||||||
|
use Ratchet\Http\HttpServer;
|
||||||
|
use Ratchet\Server\IoServer;
|
||||||
|
use Ratchet\Server\FlashPolicy;
|
||||||
|
use React\EventLoop\LoopInterface;
|
||||||
|
use React\Socket\Server as Reactor;
|
||||||
|
use React\EventLoop\Factory as LoopFactory;
|
||||||
|
use Symfony\Component\Routing\RequestContext;
|
||||||
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
||||||
|
use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An opinionated facade class to quickly and easily create a WebSocket server.
|
||||||
|
* A few configuration assumptions are made and some best-practice security conventions are applied by default.
|
||||||
|
*/
|
||||||
|
class WebSocketServer {
|
||||||
|
/**
|
||||||
|
* @var \Ratchet\Server\IoServer
|
||||||
|
*/
|
||||||
|
public $flashServer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Ratchet\Server\IoServer
|
||||||
|
*/
|
||||||
|
protected $_server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Host passed in construct used for same origin policy
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $httpHost;
|
||||||
|
|
||||||
|
/***
|
||||||
|
* The port the socket is listening
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $port;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $_routeCounter = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');`
|
||||||
|
* @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843
|
||||||
|
* @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine.
|
||||||
|
* @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you.
|
||||||
|
*/
|
||||||
|
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) {
|
||||||
|
if (extension_loaded('xdebug')) {
|
||||||
|
trigger_error('XDebug extension detected. Remember to disable this if performance testing or going live!', E_USER_WARNING);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (null === $loop) {
|
||||||
|
$loop = LoopFactory::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->httpHost = $httpHost;
|
||||||
|
$this->port = $port;
|
||||||
|
|
||||||
|
$socket = new Reactor($address . ':' . $port, $loop);
|
||||||
|
|
||||||
|
$this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher(WebSocketRouter::getRoutes(), new RequestContext))), $socket, $loop);
|
||||||
|
|
||||||
|
$policy = new FlashPolicy;
|
||||||
|
$policy->addAllowedAccess($httpHost, 80);
|
||||||
|
$policy->addAllowedAccess($httpHost, $port);
|
||||||
|
|
||||||
|
if (80 == $port) {
|
||||||
|
$flashUri = '0.0.0.0:843';
|
||||||
|
} else {
|
||||||
|
$flashUri = 8843;
|
||||||
|
}
|
||||||
|
$flashSock = new Reactor($flashUri, $loop);
|
||||||
|
$this->flashServer = new IoServer($policy, $flashSock);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the server by entering the event loop
|
||||||
|
*/
|
||||||
|
public function run() {
|
||||||
|
$this->_server->run();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue