Beispiel #1
0
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
use MyApp\Logger;
// $logger is our interface to SYSLOG services. We use it to LOG chat-server
// activities... to SYSLOG (so to be able to "tail -f syslog" to check what's going on)
$logger = new Logger();
$logger->SetDetails(false);
$logger->SetLevel(LOG_DEBUG);
// $loop is our main event-loop object
$logger->debug('Instantiating main loop object');
$loop = React\EventLoop\Factory::create();
// $chat is our main object, handling communications
// with connected browsers, via websockets;
$logger->debug('Instantiating main Chat object');
$chat = new Chat();
// this is our TCP-server that will receive messages
// to be delivered to clients/browsers via websocket;
$logger->debug('Instantiating main "Server" socket object');
$server = new React\Socket\Server($loop);
// Let's define some very-basic code to be run when something is
// received on the main socket (the one supposed to RECEIVE the
// data to be sent to websocket clients...
$logger->debug('Setting [onConnection/onData] callback');
$server->on('connection', function ($conn) use($chat, $logger) {
    $logger->info('[onConnection] New client connected');
    //I'm not going to broadcast anything, as it's not a JSON-formattted data to be shown on dashboard
    //$chat->broadcast ( "[onCONNECTION] Client arrivato..." . PHP_EOL );
    // Please note that the onDATA callback is defined INSIDE the outer onCONNECTION one!
    $conn->on('data', function ($data) use($conn, $chat, $logger) {
        $logger->debug('[onData] Got data from socket [' . $data . ' bytes]');
Beispiel #2
0
require dirname(__DIR__) . '/vendor/autoload.php';
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
use MyApp\Logger;
$logger = new Logger();
$logger->SetDetails(false);
$logger->SetLevel(LOG_INFO);
// main event-loop objects
$logger->debug('Instantiating main loop object');
$loop = React\EventLoop\Factory::create();
// this is our main object, handling communications
// with connected browsers, via websockets;
$logger->debug('Instantiating main Chat object');
$chat = new Chat();
// this is our TCP-server that will receive messages
// to be delivered to clients/browsers via websocket;
$logger->debug('Instantiating main "Server" socket object');
$server = new React\Socket\Server($loop);
// Let's define some very-basic code to be run when something is
// received on the main socket (the one supposed to RECEIVE the
// data to be sent to websocket clients...
$logger->debug('Setting [onConnection/onData] callback');
$server->on('connection', function ($conn) use($chat, $logger) {
    $logger->info('[onConnection] New client connected');
    $chat->broadcast("[onCONNECTION] Client arrivato..." . PHP_EOL);
    // Please note that the onDATA callback is defined INSIDE the outer onCONNECTION one!
    $conn->on('data', function ($data) use($conn, $chat, $logger) {
        $logger->debug('[onData] Got data from socket [' . count($data) . ' bytes]');
        // passo ai client un JSON "puro", altrimenti genero problemi di parsing...