diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index acc05a9..996f32a 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\Console; +use BeyondCode\LaravelWebSockets\Facades\WebSocketRouter; use Illuminate\Console\Command; use BeyondCode\LaravelWebSockets\Server\WebSocketServer; @@ -31,15 +32,15 @@ class StartWebSocketServer extends Command */ public function handle() { - $url = parse_url(config('app.url')); - $loop = LoopFactory::create(); - $loop->futureTick(function () use ($url) { + $loop->futureTick(function () { $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(); + // TODO: add an option to not start the echo server + WebSocketRouter::echo(); + + (new WebsocketServer($this->option('port'), '0.0.0.0', $loop))->run(); } } \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index b1693e5..98a1fb7 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -52,4 +52,6 @@ class Channel return $connection->socketId === $conn->socketId; })->each->send(json_encode($payload)); } + + //TODO: add unsubscribe } \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/Channels/ChannelManager.php b/src/LaravelEcho/Pusher/Channels/ChannelManager.php index 5db52cd..16fa54f 100644 --- a/src/LaravelEcho/Pusher/Channels/ChannelManager.php +++ b/src/LaravelEcho/Pusher/Channels/ChannelManager.php @@ -7,7 +7,7 @@ class ChannelManager { protected $channels = []; - public function findOrCreate(string $channelId) + public function findOrCreate(string $channelId): Channel { if (! isset($this->channels[$channelId])) { $channelClass = $this->detectChannelClass($channelId); diff --git a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php index 1a88de7..9029bd1 100644 --- a/src/LaravelEcho/Pusher/Channels/PresenceChannel.php +++ b/src/LaravelEcho/Pusher/Channels/PresenceChannel.php @@ -27,6 +27,8 @@ class PresenceChannel extends Channel 'channel' => $this->channelId, 'data' => json_encode($this->getChannelData()) ])); + + //TODO: send member_added message back to client, and broadcast to everyone on channel } /** diff --git a/src/Router.php b/src/Router.php index 6704b1b..927f4b6 100644 --- a/src/Router.php +++ b/src/Router.php @@ -74,11 +74,16 @@ class Router */ public function echo() { + //TODO: add orgin checker middleware + $this->get('/app/{appId}', LaravelEcho\WebSocket\EchoServer::class); + + // TODO: fleshen out http API $this->get('/apps/{appId}/status', LaravelEcho\Http\Controllers\StatusController::class); $this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class); $this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\StatusController::class); $this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\StatusController::class); + $this->post('/apps/{appId}/events', LaravelEcho\Http\Controllers\EventController::class); } diff --git a/src/Server/WebSocketServer.php b/src/Server/WebSocketServer.php index b510f99..6681469 100644 --- a/src/Server/WebSocketServer.php +++ b/src/Server/WebSocketServer.php @@ -13,77 +13,20 @@ 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; +class WebSocketServer +{ + /** @var \Ratchet\Server\IoServer */ + protected $server; + public function __construct($port = 8080, $address = '127.0.0.1', LoopInterface $loop) + { $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); + $this->server = new IoServer(new HttpServer(new Router(new UrlMatcher(WebSocketRouter::getRoutes(), new RequestContext))), $socket, $loop); } - /** - * Run the server by entering the event loop - */ - public function run() { - $this->_server->run(); + public function run() + { + $this->server->run(); } }