PHP code example of paragi / php-websocket-client
1. Go to this page and download the library: Download paragi/php-websocket-client 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/ */
paragi / php-websocket-client example snippets
try {
$sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',80) );
$sp->write("hello server");
echo "Server responded with: " . $sp->read();
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
echo "Something gets wrong ".$e->getMessage();
}
try {
$headers = ["Cookie: SID=".session_id()];
$sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',80,$headers,$errstr,16);
$bytes_written = $sp->write("hello server");
$data = $sp->read();
echo "Server responded with: ". $data;
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
echo "Something gets wrong ".$e->getMessage();
}
try {
$sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',443,'',$errstr, 10,true) ) {
$sp->write("hello server");
echo "Server responded with: " . $sp->read();
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
echo "Something gets wrong ".$e->getMessage();
}
try {
$sp = new \Paragi\PhpWebsocket\Client($this->localIp, $this->wsPort, ['X-Pusher: Symfony']);
$sp->write($data);
$reading = $sp->read();
$ret = "Server responded with: $reading";
return new JsonResponse(['level' => 'success', 'message' => $ret], Response::HTTP_OK);
} catch (ConnectionException $ex) {
return new JsonResponse(['level' => 'error', 'message' => $ex->getMessage()], Response::HTTP_SERVICE_UNAVAILABLE);
}
bash
$ php tests/bin/echoserver.php