Coder Social home page Coder Social logo

websocket's Introduction

Hoa

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\Websocket

This library allows to manipulate the WebSocket protocol and proposes a server and a client. It supports two specifications RFC6455 and Hybi (at the same time).

Quick usage

As a quick overview, we propose to start a websocket server and echo messages. The class Hoa\Websocket\Server proposes six listeners: open, message, binary-message, ping, close and error. Thus:

$websocket = new Hoa\Websocket\Server(
    new Hoa\Socket\Server('tcp://127.0.0.1:8889')
);
$websocket->on('open', function ( Hoa\Core\Event\Bucket $bucket ) {

    echo 'new connection', "\n";

    return;
});
$websocket->on('message', function ( Hoa\Core\Event\Bucket $bucket ) {

    $data = $bucket->getData();
    echo '> message ', $data['message'], "\n";
    $bucket->getSource()->send($data['message']);
    echo '< echo', "\n";

    return;
});
$websocket->on('close', function ( Hoa\Core\Event\Bucket $bucket ) {

    echo 'connection closed', "\n";

    return;
});
$websocket->run();

Finally, we have to write a client in HTML and Javascript:

<input type="text" id="input" placeholder="Message…" />
<hr />
<pre id="output"></pre>

<script>
  var host   = 'ws://127.0.0.1:8889';
  var socket = null;
  var input  = document.getElementById('input');
  var output = document.getElementById('output');
  var print  = function ( message ) {

      var samp       = document.createElement('samp');
      samp.innerHTML = message + '\n';
      output.appendChild(samp);

      return;
  };

  input.addEventListener('keyup', function ( evt ) {

      if(13 === evt.keyCode) {

          var msg = input.value;

          if(!msg)
              return;

          try {

              socket.send(msg);
              input.value = '';
              input.focus();
          }
          catch ( e ) {

              console.log(e);
          }

          return;
      }
  });

  try {

      socket = new WebSocket(host);
      socket.onopen = function ( ) {

          print('connection is opened');
          input.focus();

          return;
      };
      socket.onmessage = function ( msg ) {

          print(msg.data);

          return;
      };
      socket.onclose = function ( ) {

          print('connection is closed');

          return;
      };
  }
  catch ( e ) {

      console.log(e);
  }
</script>

Here we are. All sent messages are echoed.

Documentation

Different documentations can be found on the website: http://hoa-project.net/.

License

Hoa is under the New BSD License (BSD-3-Clause). Please, see LICENSE.

websocket's People

Contributors

hywan avatar osaris avatar jubianchi avatar

Watchers

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