0
我創建了一個PHP websocket服務器並通過webbrowser連接到它。一切正常。現在我想將一些cookie信息發送到websocket服務器,但cookie並不會使用標頭髮送。我真的不知道爲什麼cookies不包含在請求標題中。 (設置cookie,而不是空的,在index.php中PHP echo'ing時)PHP Cookie不是由websocket handshake設置的
這是請求和響應首部
請求
GET ws://127.0.0.1:9000/chat HTTP/1.1
Host: 127.0.0.1:9000
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4
Sec-WebSocket-Key: 2nR9SZYdLOG32fpV6eJ/wA==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
響應
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: QecMAdsem/PYww1pYkyjD+79OmM=
握手
// do handyshake: (hybi-10)
$secKey = $headers['Sec-WebSocket-Key'];
$secAccept = base64_encode(pack('H*', sha1($secKey . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11')));
$response = "HTTP/1.1 101 Switching Protocols\r\n";
$response.= "Upgrade: websocket\r\n";
$response.= "Connection: Upgrade\r\n";
$response.= "Sec-WebSocket-Accept: " . $secAccept . "\r\n";
連接到服務器的客戶端(JavaScript)的途徑
var socket = new WebSocket('ws://127.0.0.1:9000/chat');
非常感謝你的回覆和答案!它正在工作。很奇怪,我一直認爲localhost等於127.0.0.1,所以我用哪一個並不重要,但事實證明它不一樣。 – Piet