WebSocket::listen PHP Method

listen() public method

public listen ( )
    function listen()
    {
        while (true) {
            $changed = $this->sockets;
            socket_select($changed, $write = NULL, $except = NULL, NULL);
            foreach ($changed as $socket) {
                if ($socket == $this->master) {
                    $client = socket_accept($this->master);
                    if ($client < 0) {
                        $this->log("socket_accept() failed");
                        continue;
                    } else {
                        $this->connect($client);
                    }
                } else {
                    $bytes = @socket_recv($socket, $buffer, 2048, 0);
                    if ($bytes == 0) {
                        $this->disconnect($socket);
                    } else {
                        $user = $this->getuserbysocket($socket);
                        if (!$user->handshake) {
                            $this->dohandshake($user, $buffer);
                        } else {
                            $this->process($user, $this->unwrap($buffer));
                        }
                    }
                }
            }
        }
    }

Usage Example

#!/php -q
<?php 
// Run from command prompt > php -q websocket.demo.php
// Basic WebSocket demo echoes msg back to client
include "websocket.class.php";
$ws = new WebSocket("localhost", 12345);
$ws->listen();