2
votes

I'm doing my first test, trying to use a phpwebsocket.

I set its handshake Sec-WebSocket-Accept header value to the following:

$accept=base64_encode(SHA1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
/* $key has $buffer value */

So I run the websocket php file (that creates a connection), as all okay.

So I run my test html that contains a script that try to connect to the phpwebsocket that I'm using, and in browser console I get this error:

fc.js:43 WebSocket connection to 'ws://localhost:8000/PetStack/inc/server.php' failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

But I found this Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value with PHP, which is solved by defining buffer limit, would be my problem that?

I don't understand how to define the buffer limit, but case it's my problem, please explain me.

It wasn't me that made this phpwebsocket, I'm just using it.

Well, this is the line of variable $upgrade in a phpwebsocket file, that sets some headers.

                "HTTP/1.1 101 Web Socket Protocol Handshake\r\n".
                "Upgrade: WebSocket\r\n".
                "Connection: Upgrade\r\n".
                "WebSocket-Origin:{$origin}\r\n".
                "Sec-WebSocket-Accept:".$accept."\r\n\r\n".
                "WebSocket-Location: ws://{$host}{$resource}\r\n\r\n".
                chr(0);
1
Now I'm using substr to get Sec-Websocket-Key and keep in my variable $key; $key=substr(substr((string)$buffer,strpos ((string)$buffer,'Sec-WebSocket-Key:')),strpos((string)$buffer,' ')+1,strpos((string)$buffer,'=')+1); - but I still get the same error. :( - user5489593

1 Answers

1
votes

Your handshake looks weird.

There is not Websocket-Origin or Websocket-Locationheaders, and you are doing a double break line between the last two.

Your response should look more like this:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=

So try something like this:

"HTTP/1.1 101 Switching Protocols\r\n".
"Upgrade: WebSocket\r\n".
"Connection: Upgrade\r\n".
"Sec-WebSocket-Accept: {$accept}\r\n\r\n"