Question

First if you could tell me if there is something here that is wrong :
client:

var ws = new WebSocket('ws://localhost:9090/websocket_server.php');// ws://echo.websocket.org/echo
console.log(ws);

ws.onopen = function(e) {
    console.log("Connection open...", e);

    ws.send("Hello WebSocket!");
};

ws.onmessage = function(e) {
    if(typeof e.data === "string"){
        console.log("String message received", e, e.data);
    } else {
        console.log("Other message received", e, e.data);
    }
};

ws.onerror = function(e) {
    console.log("WebSocket Error: " , e);
};


ws.onclose = function(e) {
    console.log("Connection closed", e);
};

server:

<?php
defined('KEY_SUFFIX') ? null : define('KEY_SUFFIX', "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");

error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, 'localhost', 9090);
socket_listen($sock);

while (true) {

    $client = socket_accept($sock) or die('socket_accept returned false');;

    //$buf = socket_read($client, 1024);
    $buf = null;$key=null;
    while ( $line = socket_read($client, 2048, PHP_NORMAL_READ) ) {
        $buf .= $line;
        if ( strpos($line, 'Sec-WebSocket-Key')!== false ) {
            $key = substr($line, 19);
        } else 
        if ( strpos($line, 'User-Agent')!== false ) {
            break;
        }
    }
    //echo $buf;

    $sha1 = SHA1($key.KEY_SUFFIX, true);
    $accept = base64_encode($sha1);

    $write  = "HTTP/1.1 101 Switching Protocols\n";
    $write .= "Upgrade: websocket\n";
    $write .= "connection: Upgrade\n";
    $write .= "Sec-Websocket-Accept: $accept\n";
    //$write .= "Sec-Websocket-Extensions: extension\n";

    socket_write( $client, $write, strlen($write) );

}
socket_close($sock);
?>

I run the php script in this way:
F:\xampp\php\php -q D:\websocket_server.php

Question:
Suppose if It's all correct, I keep looking at chrome debugger network section and It's pending and I expected that after this process I should see an onopen event in js console,
what is going on? Am I expecting wrong?
Should the connection between sever and client go from 0 state to 1 state after this process or It needs more work to establish an open state?

Was it helpful?

Solution

I found the problems:
mistakes:
1: reading the last character in the line of the sec-websocket-key which made the calculation of the sec-websocket-accpet wrong on line 18

2: Didn't know 2 things about response header : one is that you need to put \r\n at the end of each line not just \n and seccond is that you need to put two \r\n\r\n after the last line of header, it just doesn't work if it's not two. (lines: 30-33)

Now it initiates an onopen event in client.

<?php
    defined('KEY_SUFFIX') ? null : define('KEY_SUFFIX', "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");

    error_reporting(E_ALL);
    set_time_limit(0);
    ob_implicit_flush();

    $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    socket_bind($sock, 'localhost', 9090);
    socket_listen($sock);

    while (true) {

        $client = socket_accept($sock) or die('socket_accept returned false');;

        //$buf = socket_read($client, 1024);
        $buf = null;$key=null;
        while ( $line = socket_read($client, 2048, PHP_NORMAL_READ) ) {
            $buf .= $line;
            if ( strpos($line, 'Sec-WebSocket-Key')!== false ) {
                $key = substr($line, 19, 24); // <== mistake num 1 here
            } else 
            if ( strpos($line, 'User-Agent')!== false ) {
                break;
            }
        }
        //echo $buf;

        $sha1 = SHA1($key.KEY_SUFFIX, true);
        $accept = base64_encode($sha1);

        //mistake num 2 here
        $write  = "HTTP/1.1 101 Switching Protocols\r\n";
        $write .= "Upgrade: websocket\r\n";
        $write .= "connection: Upgrade\r\n";
        $write .= "Sec-Websocket-Accept: $accept\r\n\r\n";


        socket_write( $client, $write, strlen($write) );

    }
    socket_close($sock);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top