Skip to content

romainneutron/SocketServer

 
 

Repository files navigation

SocketServer

Stream-powered library for creating a socket server in PHP.

Build Status

Install

The recommended way to install SocketServer is through composer.

{
    "require": {
        "igorw/socket-server": "dev-master"
    }
}

Usage

Events

Both Igorw\SocketServer\Server and Igorw\SocketServer\Connection extend événement, allowing you to bind to events.

Server

  • 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.

Connection

  • data: Triggered whenever a client sends data. Arguments: $data.
  • end: Triggered whenever a client disconnects.
  • error: Triggered when an error occurs. Arguments: $message, $conn.

Input

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.

Running

The run method will start the event loop. The server will process connections, data and input until it dies.

Example

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();

Tests

To run the test suite, you need PHPUnit.

$ phpunit

License

MIT, see LICENSE.

About

Stream-powered library for creating a socket server in PHP.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published