| 3 |
liveuser |
1 |
<?php
|
|
|
2 |
namespace Ratchet;
|
|
|
3 |
use React\EventLoop\LoopInterface;
|
|
|
4 |
use React\EventLoop\Factory as LoopFactory;
|
|
|
5 |
use React\Socket\Server as Reactor;
|
|
|
6 |
use React\Socket\SecureServer as SecureReactor;
|
|
|
7 |
use Ratchet\Http\HttpServerInterface;
|
|
|
8 |
use Ratchet\Http\OriginCheck;
|
|
|
9 |
use Ratchet\Wamp\WampServerInterface;
|
|
|
10 |
use Ratchet\Server\IoServer;
|
|
|
11 |
use Ratchet\Server\FlashPolicy;
|
|
|
12 |
use Ratchet\Http\HttpServer;
|
|
|
13 |
use Ratchet\Http\Router;
|
|
|
14 |
use Ratchet\WebSocket\MessageComponentInterface as WsMessageComponentInterface;
|
|
|
15 |
use Ratchet\WebSocket\WsServer;
|
|
|
16 |
use Ratchet\Wamp\WampServer;
|
|
|
17 |
use Symfony\Component\Routing\RouteCollection;
|
|
|
18 |
use Symfony\Component\Routing\Route;
|
|
|
19 |
use Symfony\Component\Routing\RequestContext;
|
|
|
20 |
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* An opinionated facade class to quickly and easily create a WebSocket server.
|
|
|
24 |
* A few configuration assumptions are made and some best-practice security conventions are applied by default.
|
|
|
25 |
*/
|
|
|
26 |
class App {
|
|
|
27 |
/**
|
|
|
28 |
* @var \Symfony\Component\Routing\RouteCollection
|
|
|
29 |
*/
|
|
|
30 |
public $routes;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* @var \Ratchet\Server\IoServer
|
|
|
34 |
*/
|
|
|
35 |
public $flashServer;
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @var \Ratchet\Server\IoServer
|
|
|
39 |
*/
|
|
|
40 |
protected $_server;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The Host passed in construct used for same origin policy
|
|
|
44 |
* @var string
|
|
|
45 |
*/
|
|
|
46 |
protected $httpHost;
|
|
|
47 |
|
|
|
48 |
/***
|
|
|
49 |
* The port the socket is listening
|
|
|
50 |
* @var int
|
|
|
51 |
*/
|
|
|
52 |
protected $port;
|
|
|
53 |
|
|
|
54 |
/**
|
|
|
55 |
* @var int
|
|
|
56 |
*/
|
|
|
57 |
protected $_routeCounter = 0;
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');`
|
|
|
61 |
* @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843
|
|
|
62 |
* @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine.
|
|
|
63 |
* @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you.
|
|
|
64 |
*/
|
|
|
65 |
public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) {
|
|
|
66 |
if (extension_loaded('xdebug') && getenv('RATCHET_DISABLE_XDEBUG_WARN') === false) {
|
|
|
67 |
trigger_error('XDebug extension detected. Remember to disable this if performance testing or going live!', E_USER_WARNING);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (null === $loop) {
|
|
|
71 |
$loop = LoopFactory::create();
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
$this->httpHost = $httpHost;
|
|
|
75 |
$this->port = $port;
|
|
|
76 |
|
|
|
77 |
$socket = new Reactor($address . ':' . $port, $loop);
|
|
|
78 |
|
|
|
79 |
$this->routes = new RouteCollection;
|
|
|
80 |
$this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop);
|
|
|
81 |
|
|
|
82 |
$policy = new FlashPolicy;
|
|
|
83 |
$policy->addAllowedAccess($httpHost, 80);
|
|
|
84 |
$policy->addAllowedAccess($httpHost, $port);
|
|
|
85 |
|
|
|
86 |
if (80 == $port) {
|
|
|
87 |
$flashUri = '0.0.0.0:843';
|
|
|
88 |
} else {
|
|
|
89 |
$flashUri = 8843;
|
|
|
90 |
}
|
|
|
91 |
$flashSock = new Reactor($flashUri, $loop);
|
|
|
92 |
$this->flashServer = new IoServer($policy, $flashSock);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* Add an endpoint/application to the server
|
|
|
97 |
* @param string $path The URI the client will connect to
|
|
|
98 |
* @param ComponentInterface $controller Your application to server for the route. If not specified, assumed to be for a WebSocket
|
|
|
99 |
* @param array $allowedOrigins An array of hosts allowed to connect (same host by default), ['*'] for any
|
|
|
100 |
* @param string $httpHost Override the $httpHost variable provided in the __construct
|
|
|
101 |
* @return ComponentInterface|WsServer
|
|
|
102 |
*/
|
|
|
103 |
public function route($path, ComponentInterface $controller, array $allowedOrigins = array(), $httpHost = null) {
|
|
|
104 |
if ($controller instanceof HttpServerInterface || $controller instanceof WsServer) {
|
|
|
105 |
$decorated = $controller;
|
|
|
106 |
} elseif ($controller instanceof WampServerInterface) {
|
|
|
107 |
$decorated = new WsServer(new WampServer($controller));
|
|
|
108 |
$decorated->enableKeepAlive($this->_server->loop);
|
|
|
109 |
} elseif ($controller instanceof MessageComponentInterface || $controller instanceof WsMessageComponentInterface) {
|
|
|
110 |
$decorated = new WsServer($controller);
|
|
|
111 |
$decorated->enableKeepAlive($this->_server->loop);
|
|
|
112 |
} else {
|
|
|
113 |
$decorated = $controller;
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if ($httpHost === null) {
|
|
|
117 |
$httpHost = $this->httpHost;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
$allowedOrigins = array_values($allowedOrigins);
|
|
|
121 |
if (0 === count($allowedOrigins)) {
|
|
|
122 |
$allowedOrigins[] = $httpHost;
|
|
|
123 |
}
|
|
|
124 |
if ('*' !== $allowedOrigins[0]) {
|
|
|
125 |
$decorated = new OriginCheck($decorated, $allowedOrigins);
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
//allow origins in flash policy server
|
|
|
129 |
if(empty($this->flashServer) === false) {
|
|
|
130 |
foreach($allowedOrigins as $allowedOrgin) {
|
|
|
131 |
$this->flashServer->app->addAllowedAccess($allowedOrgin, $this->port);
|
|
|
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
$this->routes->add('rr-' . ++$this->_routeCounter, new Route($path, array('_controller' => $decorated), array('Origin' => $this->httpHost), array(), $httpHost, array(), array('GET')));
|
|
|
136 |
|
|
|
137 |
return $decorated;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
/**
|
|
|
141 |
* Run the server by entering the event loop
|
|
|
142 |
*/
|
|
|
143 |
public function run() {
|
|
|
144 |
$this->_server->run();
|
|
|
145 |
}
|
|
|
146 |
}
|