<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ /** * Chat Server * * This example demonstrates how to build a simple TCP chat server which can * be connected using telnet. */ xp_import('network'); $socket = network\connect('0.0.0.0', ['port' => '8000'], function () { echo "Server Running on " . $this->socket->get_address() . PHP_EOL; }); $socket->on_connect(function (network\SIG_Connect $sig_connect) use($socket) { $sig_connect->socket->write("Welcome to the prggmr chat server" . PHP_EOL); $sig_connect->socket->write("Enter your username : "******"", explode("\r\n", $sig_read->socket->read())); // windows $content = implode("", explode("\n\r", $content)); // On first connection read in the username
<?php /** * This example demonstrates emitting signals based on network input and analysing * the emitted signals from the network to detect a wedding. * * This allows for recieving input from any input source. */ xp_import('network'); $server = network\connect('0.0.0.0', ['port' => 8000]); $server->on_read(function ($signal) { $read = trim($signal->socket->read()); if ($read == null) { return false; } xp_emit(XP_SIG($read)); }); // Once a bride, groom and bell signals are emitted we emit the wedding. $wedding = xp_complex_sig(function ($signal) { if (!isset($this->reset) || $this->reset) { $this->reset = false; $this->bride = false; $this->groom = false; $this->bells = false; } switch (true) { case $signal->compare(XP_SIG('groom')): $this->groom = true; break; case $signal->compare(XP_SIG('bride')): $this->bride = true;
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ /** * Echo Server * * This example demonstrates a simple echo server that spits back anything that * was sent and then disconnects. */ xp_import('network'); xp_import('time'); $server = network\connect('0.0.0.0', ['port' => '1337']); $server->on_connect(xp_null_exhaust(function (network\SIG_Connect $sig_connect) { if (null !== $sig_connect->socket) { echo "Connection " . PHP_EOL; echo "Closing connection in 5 seconds" . PHP_EOL; time\awake(3, function () use($sig_connect) { echo "I RAN" . PHP_EOL; $sig_connect->socket->write('Goodbye'); $sig_connect->socket->disconnect(); }); } }));
<?php echo '<pre>'; $http = http_parse_headers("GET /chat HTTP/1.1\nHost: server.example.com\nUpgrade: websocket\nConnection: Upgrade\nSec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\nSec-WebSocket-Protocol: chat, superchat\nSec-WebSocket-Version: 13\nOrigin: http://example.com"); var_dump($http); exit; xp_import('network'); $connection = network\connect('0.0.0.0', ['port' => 88]); $connection->on_connect(function ($signal) { $headers = $signal->socket->read(); var_dump($headers); // $signal->socket->disconnect(); }); $connection->on_read(function ($signal) { var_dump($this); var_dump(http_parse_headers($signal->socket->read())); });