Exemplo n.º 1
0
    $Server->log("{$ip} ({$clientID}) has connected.");
    //Send a join notice to everyone but the person who joined
    foreach ($Server->wsClients as $id => $client) {
        if ($id != $clientID) {
            $Server->wsSend($id, "Visitor {$clientID} ({$ip}) has joined the room. ---- {$client}");
        }
    }
}
// when a client closes or lost connection
function wsOnClose($clientID, $status)
{
    global $Server;
    $ip = long2ip($Server->wsClients[$clientID][6]);
    $Server->log("{$ip} ({$clientID}) has disconnected.");
    //Send a user left notice to everyone in the room
    foreach ($Server->wsClients as $id => $client) {
        $Server->wsSend($id, "Visitor {$clientID} ({$ip}) has left the room.");
    }
}
// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer('192.168.11.100', 9700);
sleep(10);
echo 'shutting down';
$Server->wsShutdownServer();