Stream-powered library for creating a socket server in PHP.
The recommended way to install SocketServer is through composer.
{
"require": {
"igorw/socket-server": "dev-master"
}
}
Both Igorw\SocketServer\Server
and Igorw\SocketServer\Connection
extend
événement, allowing you to bind to
events.
connect
: Triggered whenever a new client connects to the server. Arguments: $conn.input.*
: Triggered when custom input stream can be read. Arguments: $stream.error
: Triggered when an error occurs. Arguments: $message.
data
: Triggered whenever a client sends data. Arguments: $data.end
: Triggered whenever a client disconnects.error
: Triggered when an error occurs. Arguments: $message, $conn.
In order to communicate with the server, you can add inputs via addInput
and
bind to the input.$name
event. This allows you to trigger custom events and
run custom code when they happen.
The run
method will start the event loop. The server will process connections,
data and input until it dies.
Here is an example of a simple HTTP server listening on port 8000:
<?php
use Igorw\SocketServer\Server;
$server = new Server('localhost', 8000);
$i = 1;
$server->on('connect', function ($conn) use (&$i) {
$conn->on('data', function ($data) use ($conn, &$i) {
$lines = explode("\r\n", $data);
$requestLine = reset($lines);
if ('GET /favicon.ico HTTP/1.1' === $requestLine) {
$response = '';
$length = 0;
} else {
$response = "This is request number $i.\n";
$length = strlen($response);
$i++;
}
$conn->write("HTTP 1.1 200 OK\r\n");
$conn->write("Content-Type: text/html\r\n");
$conn->write("Content-Length: $length\r\n");
$conn->write("\r\n");
$conn->write($response);
$conn->close();
});
});
$server->run();
To run the test suite, you need PHPUnit.
$ phpunit
MIT, see LICENSE.