Skip to content

A simple PHP WebSocket implementation for PHP 5.3

Notifications You must be signed in to change notification settings

genemu/php-websocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

PHP WebSocket

A simple PHP 5.3 WebSocket server implementation that follows draft 75 and draft 76 if the WebSockets specification.

  • Supports draft 75 and draft 76 of WebSocket RFC
  • Supports Flash Socket Policy requests
  • Application module, the server can be extended by custom behaviors

Server example

This creates a server on localhost:8000 with one Application that listens on ws://localhost:8000/echo:

$server = new \WebSocket\Server('localhost', 8000);
$server->registerApplication('echo', \WebSocket\Application\EchoApplication::getInstance());
$server->run();

Example server Application

This server Applications simply echoes all messages back to all connected clients.

class EchoApplication extends Application
{
    private $clients = array();

	public function onConnect($client)
    {
        $this->clients[] = $client;
    }

    public function onDisconnect($client)
    {
        $key = array_search($client, $this->clients);
        if ($key) {
            unset($this->clients[$key]);
        }
    }

    public function onData($data, $client)
    {
        foreach ($this->clients as $sendto) {
            $sendto->send($data);
        }
    }
}

Client

var server = new WebPush('ws://localhost:8000/echo');

server.bind('open', function() {
	// Connection openend...
	server.send("Hello, I'm there!");
});

server.bind('close', function() {
	// Connection closed... 
});

server.bind('message', function(data) {
	// Data received
});	

Libraries used

About

A simple PHP WebSocket implementation for PHP 5.3

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published