2017 © Pedro Peláez
 

vcs diffsocket

A PHP Library for serving Multiple WebSocket Services in a single server

image

francium/diffsocket

A PHP Library for serving Multiple WebSocket Services in a single server

  • Friday, May 5, 2017
  • by subins2000
  • Repository
  • 1 Watchers
  • 2 Stars
  • 322 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 3 Versions
  • 5 % Grown

The README.md

DiffSocket

Build Status, (*1)

A PHP Library for serving multiple WebSocket services through a single port., (*2)

Normally, if you want to run multiple services, you would have to run WebSocket server on different ports. With DiffSocket, you can use a single port for different services., (*3)

Installation

composer require francium/diffsocket

Tutorial, (*4)

Why don't I use different ports for different services ?

Some hosting providers don't allow you to bind on multiple ports, especially if you're using a Free plan. An example is OpenShift., (*5)

I created DiffSocket, because my WebSocket server is hosted on OpenShift and needed a way to serve multiple WebSocket services through a single port., (*6)

Demos

These different services are provided through a single WebSocket port (ws-subins.rhcloud.com:8000) :, (*7)

Usage

Server

DiffSocket uses Ratchet for the WebSocket server. You should learn Ratchet to create services., (*8)

  • Configure server :, (*9)

    <?php
    $DS = new Fr\DiffSocket(array(
    "server" => array(
      "host" => "127.0.0.1",
      "port" => "8000"
    )
    ));
    
  • To add a new service, create a class under namespace Fr\DiffSocket\Service, (*10)

    SayHello.php, (*11)

    namespace Fr\DiffSocket\Service;
    
    use Ratchet\MessageComponentInterface;
    use Ratchet\ConnectionInterface;
    
    class SayHello implements MessageComponentInterface {
    
    public function onOpen(ConnectionInterface $conn){
      echo "New Connection - " . $conn->resourceId;
    }
    
    public function onClose(ConnectionInterface $conn){}
    public function onError(ConnectionInterface $conn, $error){}
    
    public function onMessage(ConnectionInterface $conn, $message){
      $conn->send("Hello");
    }
    
    }
    

    Then, you should register the service to DiffSocket by :, (*12)

    $DS->addService("say-hello", "path/to/SayHello.php");
    
  • Then, add the code to run the server, (*13)

    $DS->run();
    

You may also add services as an array when the object is made :, (*14)

$DS = new Fr\DiffSocket(array(
  "server" => array(
    "host" => "127.0.0.1",
    "port" => "8000"
  ),
  "services" => array(
    "say-hello" => __DIR__ . "/services/SayHello.php",
    "chat" => __DIR__ . "/services/Chat.php",
    "game" => __DIR__ . "/services/GameServer.php"
  )
));

Client

Just add the service name in the URL as a GET parameter. Notice the use of / before ? :, (*15)

ws://ws.example.com:8000/?service=say-hello
ws://ws.example.com:8000/?service=chat
ws://ws.example.com:8000/?service=game

An example in JavaScript :, (*16)

var sayHelloWS = new WebSocket("ws://ws.example.com:8000/?service=say-hello");
var chatWS = new WebSocket("ws://ws.example.com:8000/?service=chat");
var gameWS = new WebSocket("ws://ws.example.com:8000/?service=game");

If the GET paramater service is not passed or the value passed to it doesn't match any available services, then DiffSocket would refuse the connection and close it., (*17)

The Versions

05/05 2017

dev-master

9999999-dev

A PHP Library for serving Multiple WebSocket Services in a single server

  Sources   Download

Apache 2.0

The Requires

 

The Development Requires

by Avatar subins2000

server sockets ratchet websockets

30/05 2016

v0.1.1

0.1.1.0

A PHP Library for serving Multiple WebSocket Services in a single server

  Sources   Download

Apache 2.0

The Requires

 

The Development Requires

by Avatar subins2000

server sockets ratchet websockets

30/05 2016

v0.1

0.1.0.0

A PHP Library for serving Multiple WebSocket Services in a single server

  Sources   Download

Apache 2.0

The Requires

 

The Development Requires

by Avatar subins2000

server sockets ratchet websockets