Giter VIP home page Giter VIP logo

socket's Introduction

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.

Table of Contents

Quickstart example

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. For anything more complex, consider using the SocketClient component instead.

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

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

$loop->run();

Usage

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. The incoming connection represents the server-side end of the connection.

It MUST NOT be used to represent an outgoing connection in a client-side context. If you want to establish an outgoing connection, use the SocketClient component instead.

Install

The recommended way to install this library is through Composer. New to Composer?

This will install the latest supported version:

$ composer require react/socket:~0.4.0

If you care a lot about BC, you may also want to look into supporting legacy versions:

$ composer require "react/socket:~0.4.0|~0.3.0"

More details and upgrade guides can be found in the CHANGELOG.

License

MIT, see LICENSE file.

socket's People

Contributors

artggd avatar cboden avatar clue avatar igorw avatar sgoettschkes avatar tkleinhakisa avatar wyrihaximus avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.