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