开发者

error "not a valid Socket resource" implementing the phpwebsocket library

开发者 https://www.devze.com 2023-03-31 10:26 出处:网络
I\'m trying to work with the phpwebsocket library at http://code.google.com/p/phpwebsocket/ I\'m using the version r8 of the server.php file. For testing I just tried with the client.html file, also p

I'm trying to work with the phpwebsocket library at http://code.google.com/p/phpwebsocket/ I'm using the version r8 of the server.php file. For testing I just tried with the client.html file, also provided by the website.

When the server is started I get this:

Server Started : 2011-08-29 22:11:23
Master socket  : Resource id #4
Listening on   : www.midomain.com port 12345

But when I load the client.html file in the browser, the server shows the following errors:

Notice: Undefined variable: key1 in /home/mink500/public_html/test/server.php on line 143
Notice: Undefined variable: key2 in /home/mink500/public_html/test/server.php on line 143
Warning: socket_select(): 5 is not a valid Socket resource in /home/mink500/public_html/test/server.php on line 15

There are two variables that are not defined, and the function socket_select() returns the error "5 is not a valid Socket resource"

In the browser I get the "Disconnected" message as soon as the file is loaded.

I tried to make the server work locally using XAMPP (Apache and PHP) but I got the same errors. I also tried to change ports and follow the instructions in this issue:

http://code.google.com/p/phpwebsocket/issues/detail?id=33

But I still get the error "5 is not a valid Socket resource"

I remember that refreshing the page several times I got it running a few months ago, but now it's impossible. Besides, I need it to work all the time, not just after I refresh the page like 20 times.

I also tried with the websocket.class.php file, but this time I get an error on the client side. The browser now returns "Error during WebSocket handshake: 'Sec-WebSocket-Accept' header is missin开发者_JAVA百科g".

So, I can't make it work with old or new files, with remote or local server, with magic or a ouija board!

Any idea?

Thanks


Starting with the newest phpwebsocket client.html and server.php files I replaced both the getheaders() and dohandshake() functions with the code below works for me in newest Chrome. However it currently does not write to the browser nor stay alive after one uer comment in the chat box.

function dohandshake($user, $buffer) {
$key = null;

console("\nRequesting handshake...");
console($buffer);
console("Handshaking...");

preg_match("#Sec-WebSocket-Key: (.*?)\r\n#", $buffer, $match) && $key = $match[1];

$key .= "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
$key = sha1($key);
$key = pack('H*', $key);
$key = base64_encode($key);

$upgrade =
        "HTTP/1.1 101 Switching Protocols\r\n" .
        "Upgrade: websocket\r\n" .
        "Connection: Upgrade\r\n" .
        "Sec-WebSocket-Accept: {$key}\r\n\r\n";

socket_write($user->socket, $upgrade . chr(0), strlen($upgrade . chr(0)));
$user->handshake = true;
console($upgrade);
console("Done handshaking...");
return true;
}

function getheaders($header) {
$retVal = array();
$fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
foreach ($fields as $field) {
    if (preg_match('/([^:]+): (.+)/m', $field, $match)) {
        $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
        if (isset($retVal[$match[1]])) {
            $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
        } else {
            $retVal[$match[1]] = trim($match[2]);
        }
    }
}

if (preg_match("/GET (.*) HTTP/", $header, $match)) {
    $retVal['GET'] = $match[1];
}
return $retVal;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号