2017 © Pedro Peláez
 

library websocketserver

A WebSocket implementation for PHP 5.4

image

lexty/websocketserver

A WebSocket implementation for PHP 5.4

  • Tuesday, September 22, 2015
  • by Lexty
  • Repository
  • 1 Watchers
  • 0 Stars
  • 18 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 0 % Grown

The README.md

WebSocketServer

A WebSocket implementation for PHP. Supports RFC6455., (*1)

Requirements

Installation

Using Composer:, (*2)

composer require lexty/websocketserver

A quick example

<?php

use Lexty\WebSocketServer\Server;
use Lexty\WebSocketServer\BaseApplication;

// Make sure composer dependencies have been installed
require __DIR__ . '/vendor/autoload.php';

/**
 * chat.php
 * Send any incoming messages to all connected clients
 */
class Chat extends BaseApplication 
{
    private $clients;

    public function __construct()
    {
        $this->clients = new \SplObjectStorage;
    }

    public function onOpen(ConnectionInterface $conn, HandlerInterface $handler)
    {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, PayloadInterface $msg, HandlerInterface $handler)
    {
        if (!$msg->checkEncoding('utf-8')) {
            return;
        }
        $message = 'user #' . $from->id . ' (' . $handler->pid . '): ' . strip_tags($msg);

        foreach ($this->clients as $client) {
            $client->send($message);
        }
    }

    public function onClose(ConnectionInterface $conn = null, HandlerInterface $handler)
    {
        $this->clients->detach($conn);
    }
}

$server = new Server('0.0.0.0', 8080, '/tmp/web-socket-server.pid');
$server
    ->registerApplication('/chat', new Chat)
    ->run();
$ php chat.php
// Then some JavaScript in the browser:
var conn = new WebSocket('ws://localhost:8080/chat');
conn.onmessage = function(e) { console.log(e.data); };
conn.send('Hello Me!');

You can pass additional parameters in the server connection. For example, for authorization., (*3)

// In the browser:
var conn = new WebSocket('ws://localhost:8080/chat?user=login&auth=token');
<?php

use Lexty\WebSocketServer\BaseApplication;

class App extends BaseApplication 
{
    public function onOpen(ConnectionInterface $conn, HandlerInterface $handler)
    {
        $user = $conn->request->query['user'];
        $auth = $conn->request->query['auth'];
        if (!$user || !$auth) { // some authorization
            $conn->close();
        }
    }
}

Override library classes

For using your own implementation of classes Connection or Payload you should override they names in DI container., (*4)

<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Lexty\WebSocketServer\Applications\Chat;

$container = new ContainerBuilder;
// MyConnectionClass must implements ConnectionInterface
$container->setParameter('lexty.websocketserver.payload.class', 'MyConnectionClass');
// MyPayloadClass must implements PayloadInterface
$container->setParameter('lexty.websocketserver.connection.class', 'MyPayloadClass');

$server = new Server('localhost', 8080, '/tmp/websocketserver.pid', 1, $container)
    ->registerApplication('/chat', new Chat)
    ->run();

The Versions

22/09 2015

dev-master

9999999-dev https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

 

by Medvedev Alexandr

websocket

22/09 2015

v0.1.2+1

0.1.2.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

 

by Medvedev Alexandr

websocket

22/09 2015

v0.1.2

0.1.2.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

 

by Medvedev Alexandr

websocket

21/09 2015

v0.1.1

0.1.1.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

 

by Medvedev Alexandr

websocket

21/09 2015

v0.1.0

0.1.0.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

 

by Medvedev Alexandr

websocket

18/09 2015

v0.0.5

0.0.5.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-sockets *

 

by Medvedev Alexandr

websocket

18/09 2015

v0.0.4

0.0.4.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-sockets *

 

by Medvedev Alexandr

websocket

18/09 2015

v0.0.3

0.0.3.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-sockets *

 

by Medvedev Alexandr

websocket

18/09 2015

v0.0.2

0.0.2.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-sockets *

 

by Medvedev Alexandr

websocket

17/09 2015

0.0.1

0.0.1.0 https://github.com/Lexty/web-socket-server

A WebSocket implementation for PHP 5.4

  Sources   Download

MIT

The Requires

  • php >=5.4
  • ext-pcntl *
  • ext-sockets *

 

by Medvedev Alexandr

websocket