PHP code example of aiwhj / swoole-websocket-client

1. Go to this page and download the library: Download aiwhj/swoole-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/ */

    

aiwhj / swoole-websocket-client example snippets




$server = new swoole_websocket_server("0.0.0.0", 2345);

$server->on('open', function (swoole_websocket_server $server, $request) {
	echo "server: handshake success with fd{$request->fd}\n";
});

$server->on('message', function (swoole_websocket_server $server, $frame) {
	echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
	$server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
	echo "client {$fd} closed\n";
});

$server->start();


namespace aiwhj\swooleWsClient;

2345, '/VH4OKgcjVHZTPlFzBnVTYwExCS9RdFdwDX5VdwFiV3ADaFRjBio=');

echo '准备' . "<br>";

if (!($temp = $client->connect())) {
	var_dump($temp);
	echo "失败" . "<br>";
	echo "connect to server failed.\n";
	exit;
}
$message = json_encode(array('data' => '这是广播消息', 'to' => [1, 2, 3, 4, 5, 6, 7, 8, 9]), JSON_UNESCAPED_UNICODE);

$client->send($message);
$message = $client->recv();
echo '发送' . "<br>";
if ($message === false) {
	exit;
}
echo "Received from server: {$message}\n";

echo "Closed by server.\n";