Skip to content
Payden Sutherland edited this page Dec 2, 2013 · 9 revisions

Basic usage

<?php
//instantiate new WebSocketServer object
$ws = new WebSocketServer();
//tell the instance to bind to a host and port
$ws->bind("0.0.0.0", "8080");
//bind to ssl as well on 8081
$ws->bind_ssl("0.0.0.0", "8081", "host.key", "host.crt", "sf_bundle.crt");
//define the callbacks for onopen/onclose/onmessage events.
$ws->onopen = function($client) {
  echo "New connection: " . $client->sockfd . "\n";
};
$ws->onclose = function($client) {
  echo "Closing connection: " . $client->sockfd . "\n";
};
$ws->onmessage = function($client, $msg) {
  switch ($msg->opcode) {
    case WebSocketMessage::OPCODE_TEXT:
      //handle text message here, just echo it back.
      $client->sendText($msg->payload);
      break;
    case WebSocketMessage::OPCODE_BINARY:
      //handle binary message here, just echo it back.
      $client->sendBinary($msg->payload, $msg->payload_len);
      break;
    default:
      //this should never be reached.
      break;
  }
};
//tell the instance to run.
$ws->run();
?>

If you run the above script on from the command line, you could then play around in Chrome developer tools to see that it works properly. For example, try this from your Chrome developer console. Ubiety.net is running an SSL enabled version of the above PHP script on port 8081 for your testing pleasure. :) You can copy & paste the following directly into any WebSocket enabled browser's dev tools.

var ws = new WebSocket("wss://ubiety.net:8081");
ws.onmessage = function(e) {
  console.log("Received message from server: " + e.data);
};
ws.onopen = function() {
  ws.send("Some text to send to the server and watch it be echoed back.");
  ws.send("Some more text");
};

Classes implemented by php-ws

WebSocketServer

Methods

  • WebSocketServer::__construct()
    • Constructor, does some initializing behind the scenes
  • WebSocketServer::bind($host, $port)
    • binds a listening socket to specified host and port. IPv6 supported.
  • WebSocketServer::bind_ssl($host, $port, $keyfile, $cert_file, $chain_file = NULL)
    • binds a listening SSL enabled socket to specified host and port. IPv6 supported.
  • WebSocketServer::run()
    • runs the WebSocket server (starts the event loop). This will block until interrupted by ctrl-c.

Properties

  • WebSocketServer::onopen
    • This should be set to a callable with the signature: function($client) { }
    • $client will be an instance of WebSocketClient
  • WebSocketServer::onclose
    • This should be set to a callable with the signature: function($client) { }
    • $client will be an instance of WebSocketClient
  • WebSocketServer::onmessage
    • This should be set to a callable with the signature: function($client, $msg) { }
    • $client will be an instance of WebSocketClient
    • $msg will be an instance of WebSocketMessage

WebSocketClient

Methods

  • WebSocketClient::sendText($text)
    • sends string to client associated with this instance
  • WebSocketClient::sendBinary($bin_data, $bin_length)
    • sends binary data to client associated with this instance
  • WebSocketClient::close()
    • closes this client connection

Properties

  • WebSocketClient::sockfd
    • Socket descriptor associated with this client
    • Really only useful as a unique integer to identify client
  • WebSocketClient::address
    • String representation of client's IP address

WebSocketMessage

Methods

  • There are none

Properties

  • WebSocketMessage::opcode
    • WebSocket message opcode as per RFC 6455
    • 1 = text, 2 = binary
  • WebSocketMessage::payload
    • WebSocket message payload
    • The actual data the client sent
  • WebSocketMessage::payload_len
    • The length of the payload

Constants

  • WebSocketMessage::OPCODE_TEXT = 1
  • WebSocketMessage::OPCODE_BINARY = 2
Clone this wiki locally