I need to safe my Ratchet websocket server connection, so i analysis about it and came upon Ratchet OriginCheck. I am unable to perceive on what to do with the code they’ve offered, and might’t discover any tutorial on how am i able to apply it. I am determined, that is my final hope sorry.
listed here are the codes from http://socketo.me/docs/hello-world:
server.php
<?php
use RatchetServerIoServer;
use RatchetHttpHttpServer;
use RatchetWebSocketWsServer;
use MyAppChat;
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::manufacturing facility(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
Chat.php
<?php
namespace MyApp;
use RatchetMessageComponentInterface;
use RatchetConnectionInterface;
class Chat implements MessageComponentInterface {
protected $purchasers;
public perform __construct() {
$this->purchasers = new SplObjectStorage;
echo "SERVER STARTED";
}
public perform onOpen(ConnectionInterface $conn) {
// Retailer the brand new connection to ship messages to later
$this->clients->connect($conn);
echo "New connection! ({$conn->resourceId})n";
}
public perform onMessage(ConnectionInterface $from, $msg) {
$numRecv = rely($this->purchasers) - 1;
echo sprintf('Connection %d sending message "%s" to %d different connectionpercents' . "n"
, $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
foreach ($this->purchasers as $consumer) {
if ($from !== $consumer) {
// The sender is just not the receiver, ship to every consumer related
$client->ship($msg);
}
}
}
public perform onClose(ConnectionInterface $conn) {
// The connection is closed, take away it, as we will now not ship it messages
$this->clients->detach($conn);
echo "Connection {$conn->resourceId} has disconnectedn";
}
public perform onError(ConnectionInterface $conn, Exception $e) {
echo "An error has occurred: {$e->getMessage()}n";
$conn->shut();
}
}
And right here, the codes for OriginCheck from http://socketo.me/docs/origin
Sadly, i do not know the right way to apply this. And the way am i able to check this if it is working.
<?php
// Your shell script
use RatchetHttpOriginCheck;
use RatchetHttpHttpServer;
use RatchetServerIoServer;
$checkedApp = new OriginCheck(new MyHttpApp, array('localhost'));
$checkedApp->allowedOrigins[] = 'mydomain.com';
$server = IoServer::manufacturing facility(new HttpServer($checkedApp));
$server->run();
so the place ought to i put these codes? THANKS.