WebSocket and PHP
<p>I try to work with WebSocket in client side and with socket in server side.
I use javascript for client side and PHP for server side.</p>

<p>I write this code and i can't understand what's wrong...
It should work!</p>

<p>client side</p>

<pre class="lang-js prettyprint-override"><code>function startConnection(){
  var message = 'Hi Test';
  var port = 2516;
  var websocket = new WebSocket("ws://localhost:"+ port +"/LipariStudios/tesiLaurea/sketchApps/22/server.php");

  websocket.onopen = function(evt) {
    console.log(evt);
    websocket.send(message);
    websocket.close();
  };
  websocket.onclose = function(evt) { console.log('onclose\n' +evt); };
  websocket.onmessage = function(evt) { console.log('onmessage\n'+evt) };
  websocket.onerror = function(evt) { console.log(evt); };
}
</code></pre>

<p>in server side i try to process the socket</p>

<pre><code>// conf
$host = 'localhost';
$local = 'LipariStudios/tesiLaurea/sketchApps/22/server.php';   //  local
$data = 'risposta dalla socket';
$port = 2516;

$maxBufferSize = 2048;
$key = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)    or die("Failed: socket_create()");
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1)   or die("Failed: socket_option()");
socket_bind($socket, $host.'/'.$local, $port)             or die("Failed: socket_bind()\n");

socket_listen($socket,20)                                 or die("Failed: socket_listen()");
socket_accept($socket);

while(true){
    $numBytes = @socket_recv($socket, $buffer, $maxBufferSize, MSG_WAITALL);
    if ($numBytes === false) {
        $sockErrNo = socket_last_error($socket);
        switch ($sockErrNo){
            case 102: // ENETRESET    -- Network dropped connection because of reset
            case 125: // ECANCELED    -- Operation canceled
                break;
            default:
                echo 'Errore sconosciuto ['. $sockErrNo .']: ' . socket_strerror($sockErrNo);
        }

    }
    elseif ($numBytes == 0) {
        // disconnect
        echo "Client disconnesso. TCP connection persa: " . $socket;
    }
    else {
            $tmp = str_replace("\r", '', $buffer);
            if (strpos($tmp, "\n\n") === false ) {
                continue; 
            }

            // handshake

            // • prendi gli headers e lo componi come un array associativo e lo prendi dul buffer
            $headers = array();
            $lines = explode("\n",$buffer);
            foreach ($lines as $line) {
                if (strpos($line,":") !== false) {
                    $header = explode(":",$line,2);
                    $headers[strtolower(trim($header[0]))] = trim($header[1]);
                }
                elseif (stripos($line,"get ") !== false) {
                    preg_match("/GET (.*) HTTP/i", $buffer, $reqResource);
                    $headers['get'] = trim($reqResource[1]);
                }
            }

            // • prendi il protocolo   //  solitamente chat
            $subProtocol = (isset($headers['sec-websocket-protocol'])) ? $headers['sec-websocket-protocol'] : "";

            // • prendi il sub protocollo  //  solitamente super chat
            $extensions = (isset($headers['sec-websocket-extensions'])) ? $headers['sec-websocket-extensions'] : "";

            // • valore chiave 258EAFA5-E914-47DA-95CA-C5AB0DC85B11

            // • fai lo SHA-1 della key insieme alla chiave
            $webSocketKeyHash = sha1($headers['sec-websocket-key'] . $key);

            // • chr corrispondente ascii di un carattere
            // • hexdec da una stringa esadecimale ne ritorna il valore decimale

            $rawToken = "";
            for ($i = 0; $i &lt; 20; $i++) {
              $rawToken .= chr(hexdec(substr($webSocketKeyHash,$i*2, 2)));
            }
            $handshakeToken = base64_encode($rawToken) . "\r\n";

            // • componi la chiave
//            Sec-WebSocket-Accept: $handshakeToken . $subProtocol . $extensions\r\n

            // • componi la risposta
            $handshakeResponse = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: $handshakeToken$subProtocol$extensions\r\n";

            // • invia la risposta alla socket
            socket_write($socket,$handshakeResponse,strlen($handshakeResponse));

    }
}


socket_close($socket);
</code></pre>

<p>And this is a screenshot of appening
<a href="http://i.stack.imgur.com/87Ajr.png" rel="nofollow">chrome console</a></p>

<p><a href="http://i.stack.imgur.com/jmHpX.png" rel="nofollow">chrome network console</a></p>