Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Rocket
- {
- use Rocket\Socket\Stream;
- class Server
- {
- private $_stream;
- private $_host = '127.0.0.1';
- private $_port = '8080';
- public function __construct($host = null, $port = null)
- {
- if ($host) { $this->_host = $host; }
- if ($port) { $this->_port = $port; }
- $this->_stream = new Stream($this->_host, $this->_port);
- }
- public function stream($cb = null)
- {
- if ($cb) {
- $this->_stream->connection()->send($cb);
- }
- return $this->_stream;
- }
- public function close()
- {
- fclose($this->_stream);
- }
- }
- }
- namespace Rocket\Socket
- {
- class Stream
- {
- private $_socket;
- public function __construct($host, $port)
- {
- $this->_socket = stream_socket_server("tcp://${host}:${port}", $errno, $errstr);
- }
- public function connection()
- {
- while (true) {
- $stream = stream_socket_accept($this->_socket, -1);
- $cb = yield;
- $cb($stream);
- }
- }
- }
- }
- namespace
- {
- // Запускаеам:
- $server = new Rocket\Server('0.0.0.0', '8080');
- while (true) {
- $server->stream(function($connection){
- fwrite($connection, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n\r\nHello world!");
- fclose($connection);
- });
- }
- $server->stream()->close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement