Skip to content

timgws/socket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket Component

Build Status

Library for building an evented socket server.

The socket component provides a more usable interface for a socket-layer server or client based on the EventLoop and Stream components.

Server

The server can listen on a port and will emit a connection event whenever a client connects.

Connection

The connection is a readable and writable stream. It can be used in a server or in a client context.

Usage

Here is a server that closes the connection if you send it anything.

    $loop = React\EventLoop\Factory::create();

    $socket = new React\Socket\Server($loop);
    $socket->on('connection', function ($conn) {
        $conn->write("Hello there!\n");
        $conn->write("Welcome to this amazing server!\n");
        $conn->write("Here's a tip: don't say anything.\n");

        $conn->on('data', function ($data) use ($conn) {
            $conn->close();
        });
    });
    $socket->listen(1337);

    $loop->run();

You can change the host the socket is listening on through a second parameter provided to the listen method:

    $socket->listen(1337, '192.168.0.1');

Here's a client that outputs the output of said server and then attempts to send it a string.

    $loop = React\EventLoop\Factory::create();

    $client = stream_socket_client('tcp://127.0.0.1:1337');
    $conn = new React\Socket\Connection($client, $loop);
    $conn->pipe(new React\Stream\Stream(STDOUT, $loop));
    $conn->write("Hello World!\n");

    $loop->run();

Advanced Usage

Disabling TCP Nagle

Nagle helps to improve the efficiency of TCP communication by reducing the number of packets that need to be sent over the network.

This helps to reduce TCP overheads.

Sometimes you want to disable Nagle to ensure that data will be sent immediately.

    $socket = new React\Socket\Server($loop);
    $socket->setTCPOption(TCP_NODELAY, true);
    $socket->on('connection', function ($conn) {
        $conn->write("This data will be sent.\n");
        usleep(200);
        $conn->write("... after 200ms!\n");
        $conn->close();
    });
    $socket->listen(1337);

Setting SO_REUSEADDR

The SO_REUSEADDR socket option allows a socket to forcibly bind to a port in use by another socket. This means multiple processes could be launched listening on the same port.

    $socket->setSocketOption(SO_REUSEADDR, true);

About

Asynchronous socket server

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy