PHP code example of gevman / s2c-websocket

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

    

gevman / s2c-websocket example snippets




$listener = new \Gevman\PhpSocket\Listener('example.com', 8080, '/socket/test', 'id', false);

$listener->events->onError(function(Exception $e) {
    printf('error: %s in %s at line %s', $e->getMessage(), $e->getFile(), $e->getLine());
});

$listener->events->onClientConnect(function($identity) {
    printf("Client connected: %s\n\n", $identity['id']);
});

$listener->events->onClientDisconnect(function($identity) {
    printf("Client disconnected: %s\n\n", $identity['id']);
});

$listener->events->onMessage(function($identities, $message) {
    printf("new message: %s to %s\n\n", $message, implode(',', array_column($identities, 'id')));
});

$listener->events->onUpdate(function($AllIdentities) {
    file_put_contents('online-users.txt', array_column($AllIdentities, 'id'));
});

$listener->listen();



$notifier = new \Gevman\PhpSocket\Notifier('example.com', 8080);
$notifier->notify(['something' => 'happened', 'new' => 'message'], 1);