58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
use Ratchet\MessageComponentInterface;
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
|
|
|
class MyChat implements MessageComponentInterface {
|
|
protected $clients;
|
|
|
|
public function __construct() {
|
|
$this->clients = new \SplObjectStorage;
|
|
}
|
|
|
|
public function onOpen(ConnectionInterface $conn) {
|
|
$this->clients->attach($conn);
|
|
}
|
|
|
|
public function onMessage(ConnectionInterface $from, $msg) {
|
|
foreach ($this->clients as $client) {
|
|
if ($from != $client) {
|
|
$client->send($msg);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function onClose(ConnectionInterface $conn) {
|
|
$this->clients->detach($conn);
|
|
}
|
|
|
|
public function onError(ConnectionInterface $conn, \Exception $e) {
|
|
$conn->close();
|
|
}
|
|
}
|
|
|
|
// Make sure composer dependencies have been installed
|
|
// require __DIR__ . '/vendor/autoload.php';
|
|
require '../vendor/autoload.php';
|
|
$app = new \Ratchet\Http\HttpServer(
|
|
new \Ratchet\WebSocket\WsServer(
|
|
new MyChat()
|
|
)
|
|
);
|
|
|
|
$loop = \React\EventLoop\Factory::create();
|
|
|
|
$secure_websockets = new \React\Socket\Server('99.192.174.19:8080', $loop);
|
|
$secure_websockets = new \React\Socket\SecureServer($secure_websockets, $loop, [
|
|
'local_cert' => '/etc/letsencrypt/live/mojo.bateworld.com-0001/cert.pem',
|
|
'local_pk' => '/etc/letsencrypt/live/mojo.bateworld.com-0001/privkey.pem',
|
|
'verify_peer' => false
|
|
]);
|
|
|
|
$secure_websockets_server = new \Ratchet\Server\IoServer($app, $secure_websockets, $loop);
|
|
$secure_websockets_server->run(); |