1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets; |
4
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Server\HttpServer; |
6
|
|
|
use BeyondCode\LaravelWebSockets\Server\Loggers\HttpLogger; |
7
|
|
|
use Ratchet\Http\Router; |
8
|
|
|
use Ratchet\Server\IoServer; |
9
|
|
|
use React\EventLoop\Factory as LoopFactory; |
10
|
|
|
use React\EventLoop\LoopInterface; |
11
|
|
|
use React\Socket\SecureServer; |
12
|
|
|
use React\Socket\Server; |
13
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
14
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher; |
15
|
|
|
use Symfony\Component\Routing\RequestContext; |
16
|
|
|
use Symfony\Component\Routing\RouteCollection; |
17
|
|
|
|
18
|
|
|
class ServerFactory |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The host the server will run on. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $host = '127.0.0.1'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The port to run on. |
29
|
|
|
* |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
protected $port = 8080; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The event loop instance. |
36
|
|
|
* |
37
|
|
|
* @var \React\EventLoop\LoopInterface |
38
|
|
|
*/ |
39
|
|
|
protected $loop; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The routes to register. |
43
|
|
|
* |
44
|
|
|
* @var \Symfony\Component\Routing\RouteCollection |
45
|
|
|
*/ |
46
|
|
|
protected $routes; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Console output. |
50
|
|
|
* |
51
|
|
|
* @var Symfony\Component\Console\Output\OutputInterface |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
protected $consoleOutput; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Initialize the class. |
57
|
|
|
* |
58
|
|
|
* @param string $host |
59
|
|
|
* @param int $port |
60
|
|
|
* @return void |
61
|
|
|
*/ |
62
|
|
|
public function __construct(string $host, int $port) |
63
|
|
|
{ |
64
|
|
|
$this->host = $host; |
65
|
|
|
$this->port = $port; |
66
|
|
|
|
67
|
|
|
$this->loop = LoopFactory::create(); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add the routes. |
72
|
|
|
* |
73
|
|
|
* @param \Symfony\Component\Routing\RouteCollection $routes |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function withRoutes(RouteCollection $routes) |
77
|
|
|
{ |
78
|
|
|
$this->routes = $routes; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set the loop instance. |
85
|
|
|
* |
86
|
|
|
* @param \React\EventLoop\LoopInterface $loop |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function setLoop(LoopInterface $loop) |
90
|
|
|
{ |
91
|
|
|
$this->loop = $loop; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Set the console output. |
98
|
|
|
* |
99
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $consoleOutput |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setConsoleOutput(OutputInterface $consoleOutput) |
103
|
|
|
{ |
104
|
|
|
$this->consoleOutput = $consoleOutput; |
|
|
|
|
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Set up the server. |
111
|
|
|
* |
112
|
|
|
* @return \Ratchet\Server\IoServer |
113
|
|
|
*/ |
114
|
|
|
public function createServer(): IoServer |
115
|
|
|
{ |
116
|
|
|
$socket = new Server("{$this->host}:{$this->port}", $this->loop); |
|
|
|
|
117
|
|
|
|
118
|
|
|
if (config('websockets.ssl.local_cert')) { |
119
|
|
|
$socket = new SecureServer($socket, $this->loop, config('websockets.ssl')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$app = new Router( |
123
|
|
|
new UrlMatcher($this->routes, new RequestContext) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
$httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024); |
127
|
|
|
|
128
|
|
|
if (HttpLogger::isEnabled()) { |
129
|
|
|
$httpServer = HttpLogger::decorate($httpServer); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return new IoServer($httpServer, $socket, $this->loop); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|