PHP code example of valtzu / guzzle-websocket-middleware
1. Go to this page and download the library: Download valtzu/guzzle-websocket-middleware library. Choose the download type require. 2. Extract the ZIP file and open the index.php. 3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
valtzu / guzzle-websocket-middleware example snippets
$handlerStack = new HandlerStack(new StreamHandler());
$handlerStack->unshift(new WebSocketMiddleware());
$guzzle = new Client(['handler' => $handlerStack]);
$handshakeResponse = $guzzle->request('GET', 'wss://ws.postman-echo.com/raw');
$ws = $handshakeResponse->getBody();
$ws->write("Hello world");
$helloWorld = $ws->read(); // This will block until the reply frame is received
$handlerStack = new HandlerStack(new StreamHandler());
$handlerStack->unshift(new WebSocketMiddleware());
$guzzle = new Client(['handler' => $handlerStack]);
$handshakeResponse = $guzzle->requestAsync('GET', 'wss://ws.postman-echo.com/raw')->wait();
$ws = $handshakeResponse->getBody();
$ws->write("Hello world");
$helloWorld = $ws->read(); // Here you may get an empty string if data wasn't received yet
$ws->ping();