PHP code example of tansoft / websocketclient

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

    

tansoft / websocketclient example snippets




class MyHandler implements WebSocket\ISocketHandler{
    public function onConnect(&$client){
        //init send something
        $client->send($reg);
    }
    public function onReceive(&$client, $msg){
        //receive something with $msg
        //return false for exit
        return true;
    }
    public function onError(&$client, $errno, $errmsg){
        echo('socket error '.$errno.':'.$errmsg);
        //return true for auto reconnect
        return true;
    }
}

$client = new WebSocket\WebSocketClient('ws://echo.websocket.org/', new MyHandler);
//setup auto ack settings
$client->setupAck('{"event":"ping"}', '{"event":"pong"}', KEEPALIVE_TIMEOUT_SECOND);

//setup rawdata record
$client->setupRawLog('rawdata.log');
$client->loop();

//or

//setup rawdata playback
//use flag WebSocket\WebSocketClient::RAWLOG_PLAYBACKMODE_QUICK for playback with no sleep
$client->setupRawLog('rawdata.log', WebSocket\WebSocketClient::RAWLOG_PLAYBACKMODE_NORMAL);
$client->loop();