humanmade/phpws

Name: phpws

Owner: Human Made

Description: PHP Web Socket server

Created: 2015-02-19 01:07:33.0

Updated: 2015-03-24 18:32:59.0

Pushed: 2015-02-19 01:29:56.0

Homepage: null

Size: 414

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

WebSocket Server and Client library for PHP. Works with the latest HyBi specifications, as well the older Hixie #76 specification used by older Chrome versions and some Flash fallback solutions.

This project was started to bring more interactive features to http://www.u2start.com/

Features

Server

Client

Getting started

The easiest way to set up PHPWS is by using it as Composer dependency. Add the following to your composer.json


"repositories": [
    {
        "type": "vcs",
        "url": "https://github.com/Devristo/phpws"
    }
],
"require": {
    "devristo/phpws": "dev-master"
}

And run `php composer.phar install`

To verify it is working create a time.php in your project root

ire_once("vendor/autoload.php");
Devristo\Phpws\Server\WebSocketServer;

p = \React\EventLoop\Factory::create();

reate a logger which writes everything to the STDOUT
ger = new \Zend\Log\Logger();
ter = new Zend\Log\Writer\Stream("php://output");
ger->addWriter($writer);

reate a WebSocket server using SSL
ver = new WebSocketServer("tcp://0.0.0.0:12345", $loop, $logger);

p->addPeriodicTimer(0.5, function() use($server, $logger){
$time = new DateTime();
$string = $time->format("Y-m-d H:i:s");
$logger->notice("Broadcasting time to all clients: $string");
foreach($server->getConnections() as $client)
    $client->sendString($string);



ind the server
ver->bind();

tart the event loop
p->run();

And a client time.html as follows

l>
<head>
    <title>WebSocket TEST</title>
</head>
<body>
    <h1>Server Time</h1>
    <strong id="time"></strong>

    <script>
        var socket = new WebSocket("ws://localhost:12345/");
        socket.onmessage = function(msg) {
            document.getElementById("time").innerText = msg.data;
        };
    </script>
</body>
ml>

Now run the time.php from the command line and open time.html in your browser. You should see the current time, broadcasted by phpws at regular intervals. If this works you might be interested in more complicated servers in the examples folder.

Getting started with the Phpws Client

The following is a client for the websocket server hosted at http://echo.websocket.org

ire_once("vendor/autoload.php");                // Composer autoloader

p = \React\EventLoop\Factory::create();

ger = new \Zend\Log\Logger();
ter = new Zend\Log\Writer\Stream("php://output");
ger->addWriter($writer);

ent = new \Devristo\Phpws\Client\WebSocket("ws://echo.websocket.org/?encoding=text", $loop, $logger);

ent->on("request", function($headers) use ($logger){
$logger->notice("Request object created!");


ent->on("handshake", function() use ($logger) {
$logger->notice("Handshake received!");


ent->on("connect", function($headers) use ($logger, $client){
$logger->notice("Connected!");
$client->send("Hello world!");


ent->on("message", function($message) use ($client, $logger){
$logger->notice("Got message: ".$message->getData());
$client->close();


ent->open();
p->run();

Known Issues

Requirements

Server

Client


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.