0

I am new in websockets technology. I was trying to create a websockets php server and connect to the server with a javascript client. I am using xampp 1.8.3.

I made this simple PHP server:

<?php 
    error_reporting(E_ALL);
    set_time_limit(0);

    $address = "127.0.0.1";
    $port = 1777;
    $maxConnections = 10;

    if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))){
        $errorCode = socket_last_error();
        $errorMsg = socket_strerror($errorCode);
        die("socket_create() failed -> {$errorCode}:{$errorMsg}\n");
    }
    echo "Server created.\n";

    if(!(socket_bind($sock, $address, $port))){
        $errorCode = socket_last_error();
        $errorMsg = socket_strerror($errorCode);
        die("socket_bind() failed -> {$errorCode}:{$errorMsg}\n");
    }
    echo "Server opened on {$address}:{$port}\n";

    if(!(socket_listen($sock, $maxConnections))){
        $errorCode = socket_last_error();
        $errorMsg = socket_strerror($errorCode);
        die("socket_listen() failed -> {$errorCode}:{$errorMsg}\n");
    }
    echo "Waiting for connections...\n";

    $client = socket_accept($sock);
    if(socket_getpeername($client, $address, $port)){
        echo "The client {$address}:{$port} is online.\n";
    }
    $msg = socket_read($client, 1024000);
    if(!(socket_write($client, $msg, strlen($msg)))){
        $errorCode = socket_last_error();
        $errorMsg = socket_strerror($errorCode);
        die("socket_write() failed -> {$errorCode}:{$errorMsg}\n");
    }
    echo "Message sent\n";

    socket_close($client);
    socket_close($sock);


?>

I ran this php file with xampp shell using the following expression:

php htdocs/server.php

and I got this message on shell:

php htdocs/server.php
Server created.
Server opened on 127.0.0.1:1777
Waiting for connections...

Then I opened my client.html on chrome (my supports websockets). My client.html code is:

<html>
    <head>
        <meta charset="UTF-8" />
        <title>Websockets web-server connection</title>
    </head>
    <body>
        <p>Websockets connection. Status: <span id="status"></span></p>
        <script type="text/javascript">
            var webSockets = new WebSocket("ws://127.0.0.1:1777/");
            webSockets.onopen = function(){
                document.getElementById("status").innerHTML="connected";
                webSockets.send("ping");
            }
        </script>
    </body>
</html>

And I received this message on javascript console log:

WebSocket connection to 'ws://127.0.0.1:1777/' failed:  client.html:9

And my shell had this messages:

php htdocs/server.php
Server created.
Server opened on 127.0.0.1:1777
Waiting for connections...
The client 127.0.0.1:64446 is online
Message sent

However I didn't receive any message on client, I do not even get a connected status.

What is happening? Where is my error?

Thank you.

2 Answers 2

4

WebSockets are not raw TCP sockets. They require a rather complex HTTP-like handshake to establish a connection, and require data transferred over them to be encoded and framed in a very particular way. The protocol is defined in RFC 6455.

Unless you are feeling incredibly masochistic, you don't want to try to implement this yourself. Use a library like Ratchet to implement WebSockets in PHP.

Sign up to request clarification or add additional context in comments.

Comments

3

I had the same problem bro but no need of using Ratchet or other libraries you can write your own simple code . The handshake process,and the masking-unmasking of messages is rather difficult so i copied the code for those

function perform_handshaking($receved_header,$client_conn, $host, $port)
{
    $headers = array();
    $lines = preg_split("/\r\n/", $receved_header);
    foreach($lines as $line)
    {
        $line = chop($line);
        if(preg_match('/\A(\S+): (.*)\z/', $line, $matches))
        {
            $headers[$matches[1]] = $matches[2];
        }
    }

    $secKey = $headers['Sec-WebSocket-Key'];
    $secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
    //hand shaking header
    $upgrade  = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n" .
    "Upgrade: websocket\r\n" .
    "Connection: Upgrade\r\n" .
    "WebSocket-Origin: $host\r\n" .
    "WebSocket-Location: ws://$host:$port/\r\n".
    "Sec-WebSocket-Accept:$secAccept\r\n\r\n";
    socket_write($client_conn,$upgrade,strlen($upgrade));
    return $upgrade;
}

function unmask($text) {
    $length = ord($text[1]) & 127;
    if($length == 126) {
        $masks = substr($text, 4, 4);
        $data = substr($text, 8);
    }
    elseif($length == 127) {
        $masks = substr($text, 10, 4);
        $data = substr($text, 14);
    }
    else {
        $masks = substr($text, 2, 4);
        $data = substr($text, 6);
    }
    $text = "";
    for ($i = 0; $i < strlen($data); ++$i) {
        $text .= $data[$i] ^ $masks[$i%4];
    }
    return $text;
}

//Encode message for transfer to client.
function mask($text)
{
    $b1 = 0x80 | (0x1 & 0x0f);
    $length = strlen($text);

    if($length <= 125)
        $header = pack('CC', $b1, $length);
    elseif($length > 125 && $length < 65536)
        $header = pack('CCn', $b1, 126, $length);
    elseif($length >= 65536)
        $header = pack('CCNN', $b1, 127, $length);
    return $header.$text;
}

Instead of socket_accept user socket_read to get the http header containing request from webpage that it wants to upgrade its http conn to websocket then use the handshake function above to write an accept header message .then your connection will be established .but still on the client side you have to add events like these

if("WebSocket" in window){
    var a = "ws://"+serverip+":9000";
    var ws = new WebSocket(a);
    var error = null;

    ws.onopen = function(){
        open();
    }

    ws.onerror = function(err){
        errorhandler(err);

    }
    ws.onmessage = function(e){
        messagehandler(e);

    }
    ws.onclose = function(){
        close();
    }

}

function  open(){
               document.getElementById('logs').innerHTML+='<p>WebSocket Connection OPened</p>';

}

function errorhandler(err){
               document.getElementById('logs').innerHTML+='<p>WebSocket Connection Error occured &nbsp'+err.data+'</p>';

}

function messagehandler(a){
               document.getElementById('logs').innerHTML+="<p>"+a.data+"</p>";

}

function close(){
                document.getElementById('logs').innerHTML+='<p>WebSocket Connection Closed</p>';
                ws = null;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.