This commit is contained in:
freek 2018-11-21 21:23:25 +01:00
parent 2344c7b704
commit d6b6182c39
6 changed files with 26 additions and 73 deletions

View File

@ -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();
}
}

View File

@ -52,4 +52,6 @@ class Channel
return $connection->socketId === $conn->socketId;
})->each->send(json_encode($payload));
}
//TODO: add unsubscribe
}

View File

@ -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);

View File

@ -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
}
/**

View File

@ -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);
}

View File

@ -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();
}
}