To connect to wss://echo.websocket.org
, using php, without libraries:
<?php
$sock = stream_socket_client("ssl://echo.websocket.org:443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));
if(!$sock){
echo"[$n]$e".PHP_EOL;
} else {
fwrite($sock,"GET / HTTP/1.1\r\nHost: echo.websocket.org\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");
while(!feof($sock)){
var_dump(fgets($sock,2048));
}
}
This handles the key handshake for the wss encrypted flux.
Response should looks like:
HTTP/1.1 101 Web Socket Protocol Handshake
Connection: Upgrade
Date: Fri, 15 Nov 2019 05:15:36 GMT
Sec-WebSocket-Accept: wl5g9FOLmrZUguiPZeig/2uiI9o=
Server: Kaazing Gateway
Upgrade: websocket
Same code as a command line:
php -r '$sock=stream_socket_client("ssl://echo.websocket.org:443",$e,$n,30,STREAM_CLIENT_CONNECT,stream_context_create(null));if(!$sock){echo"[$n]$e".PHP_EOL;}else{fwrite($sock,"GET / HTTP/1.1\r\nHost: echo.websocket.org\r\nAccept: */*\r\nConnection: Upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: ".rand(0,999)."\r\n\r\n");while(!feof($sock)){var_dump(fgets($sock,2048));}}'
See other similar usages here