PHP code example of danielealbano / php-websocket-s2c

1. Go to this page and download the library: Download danielealbano/php-websocket-s2c 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/ */

    

danielealbano / php-websocket-s2c example snippets




$ws = new WebsocketS2C\WebSocket();

if ($ws->isWebsocket() == false) {
	echo 'Not a websocket!';
	die();
}

$ws->accept();

$ws->text(utf8_encode('This is a test!'));

for($i = 0; $i < 3; $i++) {
	$ws->text(utf8_encode('Loop ' . $i));
	sleep(1);
}

$ws->json([
	'hello' => 'world'
]);

$ws->binary('This is a test!');

$isFirst = true;
$ws->text('[', false, false);
foreach([ 'this', 'is', 'a', 'multipart', 'message' ] as $part) {
	if ($isFirst == false) {
		$ws->text(',', false, false);
	}

	$ws->json([
		'alot' => true,
		'of' => true,
		'data' => $part
	], false);

	$isFirst = false;
}
$ws->text(']');