1. Go to this page and download the library: Download nekland/woketo 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/ */
nekland / woketo example snippets
use Your\Namespace\YourMessageHandler;
use Nekland\Woketo\Server\WebSocketServer;
$server = new WebSocketServer(1337);
$server->setMessageHandler(new YourMessageHandler(), '/path'); // accessible on ws://127.0.0.1:1337/path
$server->start(); // And that's all <3
// YourMessageHandler.php
namespace Your\Namespace;
use Nekland\Woketo\Core\AbstractConnection;
use Nekland\Woketo\Message\TextMessageHandler;
class YourMessageHandler extends TextMessageHandler
{
public function onConnection(AbstractConnection $connection)
{
// Doing something when the client is connected ?
// This method is totally optional.
}
public function onMessage(string $data, AbstractConnection $connection)
{
// Sending back the received data
$connection->write($data);
}
public function onDisconnect(AbstractConnection $connection)
{
// Doing something when the connection between client/server is disconnecting
// Optionnal
}
}