Git Product home page Git Product logo

francium-diffsocket's Introduction

DiffSocket

Build Status

A PHP Library for serving multiple WebSocket services through a single port.

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.

Installation

composer require francium/diffsocket

Tutorial

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.

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

Demos

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

Usage

Server

DiffSocket uses Ratchet for the WebSocket server. You should learn Ratchet to create services.

  • Configure server :

    <?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

    SayHello.php

    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 :

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

    $DS->run();

You may also add services as an array when the object is made :

$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 ? :

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 :

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.

francium-diffsocket's People

Contributors

subins2000 avatar laupkram avatar

Watchers

James Cloos avatar Tirapong Chaiyakun 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.