1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Server; |
4
|
|
|
|
5
|
|
|
use Ratchet\Http\Router; |
|
|
|
|
6
|
|
|
use React\Socket\Server; |
7
|
|
|
use Ratchet\Server\IoServer; |
8
|
|
|
use React\Socket\SecureServer; |
9
|
|
|
use React\EventLoop\LoopInterface; |
10
|
|
|
use React\EventLoop\Factory as LoopFactory; |
11
|
|
|
use Symfony\Component\Routing\RequestContext; |
12
|
|
|
use Symfony\Component\Routing\RouteCollection; |
13
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; |
16
|
|
|
|
17
|
|
|
class WebSocketServerFactory |
18
|
|
|
{ |
19
|
|
|
/** @var string */ |
20
|
|
|
protected $host = '127.0.0.1'; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $port = 8080; |
24
|
|
|
|
25
|
|
|
/** @var \React\EventLoop\LoopInterface */ |
26
|
|
|
protected $loop; |
27
|
|
|
|
28
|
|
|
/** @var \Symfony\Component\Routing\RouteCollection */ |
29
|
|
|
protected $routes; |
30
|
|
|
|
31
|
|
|
/** @var Symfony\Component\Console\Output\OutputInterface */ |
32
|
|
|
protected $consoleOutput; |
33
|
|
|
|
34
|
|
|
public function __construct() |
35
|
|
|
{ |
36
|
|
|
$this->loop = LoopFactory::create(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function useRoutes(RouteCollection $routes) |
40
|
|
|
{ |
41
|
|
|
$this->routes = $routes; |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function setHost(string $host) |
47
|
|
|
{ |
48
|
|
|
$this->host = $host; |
49
|
|
|
|
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function setPort(string $port) |
54
|
|
|
{ |
55
|
|
|
$this->port = $port; |
|
|
|
|
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function setLoop(LoopInterface $loop) |
61
|
|
|
{ |
62
|
|
|
$this->loop = $loop; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function setConsoleOutput(OutputInterface $consoleOutput) |
68
|
|
|
{ |
69
|
|
|
$this->consoleOutput = $consoleOutput; |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function createServer(): IoServer |
75
|
|
|
{ |
76
|
|
|
$socket = new Server("{$this->host}:{$this->port}", $this->loop); |
77
|
|
|
|
78
|
|
|
if (config('websockets.ssl.local_cert')) { |
79
|
|
|
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$urlMatcher = new UrlMatcher($this->routes, new RequestContext); |
83
|
|
|
|
84
|
|
|
$router = new Router($urlMatcher); |
85
|
|
|
|
86
|
|
|
$app = new OriginCheck($router, config('websockets.allowed_origins', [])); |
87
|
|
|
|
88
|
|
|
$httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024); |
89
|
|
|
|
90
|
|
|
if (HttpLogger::isEnabled()) { |
91
|
|
|
$httpServer = HttpLogger::decorate($httpServer); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return new IoServer($httpServer, $socket, $this->loop); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: