PHP code example of grok-s / socket
1. Go to this page and download the library: Download grok-s/socket 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/ */
grok-s / socket example snippets
rok\Socket\Server;
$server = new Server('127.0.0.1', 8282);
// $server->setEncryptionKey('your-secret-key-here'); // Optional: AES-256
// Basic event handling
$server->on('connection', function($socket) {
echo "Client connected: " . $socket->getId() . "\n";
$socket->emit('welcome', 'Hello from GrokSocket!');
});
$server->on('chat', function($client, $data) use ($server) {
echo "Message from " . $client->getId() . ": " . (is_string($data) ? $data : json_encode($data)) . "\n";
});
$server->on('disconnect', function($client) {
echo "Client disconnected: " . $client->getId() . "\n";
});
$server->run(); // Starts the server