PHP code example of yaro / socket
1. Go to this page and download the library: Download yaro/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/ */
yaro / socket example snippets
'providers' => array(
//...
Yaro\Socket\ServiceProvider::class,
//...
),
'aliases' => array(
//...
'Socket' => Yaro\Socket\Facade::class,
//...
),
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
class ChatCommand extends Command
{
protected $name = 'socket:chat';
protected $description = "chat command";
public function fire()
{
Socket::init($this->argument('action'), array(
'class' => 'ChatWebsocketDaemonHandler',
'pid' => '/tmp/websocket_chat.pid',
'websocket' => 'tcp://127.0.0.1:8000',
//'localsocket' => 'tcp://127.0.0.1:8010',
//'master' => 'tcp://127.0.0.1:8020',
//'eventDriver' => 'event'
));
} // end fire
protected function getArguments()
{
return array(
array('action', InputArgument::REQUIRED, 'start|stop|restart'),
);
} // end getArguments
protected function getOptions()
{
return array();
} // end getOptions
}
class ChatWebsocketDaemonHandler extends WebsocketDaemon
{
protected function onOpen($connectionId, $info)
{
}
protected function onClose($connectionId)
{
}
protected function onMessage($connectionId, $data, $type)
{
if (!strlen($data)) {
return;
}
$message = 'user #'. $connectionId .' ('. $this->pid .'): '. strip_tags($data);
foreach ($this->clients as $idClient => $client) {
$this->sendToClient($idClient, $message);
}
}
}
shell
php artisan socket:chat