# cd /usr/ports/devel/php5-pcntl # make install clean
# cd /usr/ports/devel/php5-shmop # make install clean
# cd /usr/ports/net/php5-sockets # make install clean
# cd /usr/ports/lang/php5-extensions # make config
# make install clean
# cd /usr/ports/devel/pecl-eio # make install clean
# cd /usr/ports/devel/pecl-event # make install clean
# cd /usr/ports/devel/git # make install clean
# cd /usr/local # git clone git://github.com/kakserpom/phpdaemon.git # chmod +x phpdaemon/bin/phpd # ln -s /usr/local/phpdaemon/bin/phpd /usr/bin/phpd
/usr/local/phpdaemon/conf/phpd.conf
: user www; group www; max-workers 8; min-workers 1; start-workers 1; max-idle 0; Pool:Servers\WebSocket { enable 1; listen "tcp://0.0.0.0"; listen-port 8047; privileged; } ExampleWebSocket {}
# phpd start
M#7964 \PHPDaemon\Core\Pool:Servers\WebSocket up. M#7964 \PHPDaemon\Core\Pool:\PHPDaemon\Servers\WebSocket\Pool up. W#7966 \PHPDaemon\Examples\ExampleWebSocket up. Spawning 1 worker(s) W#7967 \PHPDaemon\Examples\ExampleWebSocket up.
<?php namespace PHPDaemon\Applications; class MyWebSocket extends \PHPDaemon\Core\AppInstance { public $enableRPC=true; // public $sessions=array(); // // public function onReady() { $appInstance = $this; // timerTask() 5 $this->timerTask($appInstance); // ws://site.com:8047/myws \PHPDaemon\Servers\WebSocket\Pool::getInstance()->addRoute('myws', function ($client) use ($appInstance) { $session=new MyWebSocketRoute($client, $appInstance); // $session->id=uniqid(); // ID $this->sessions[$session->id]=$session; // return $session; }); } function timerTask($appInstance) { // foreach($this->sessions as $id=>$session) { $session->client->sendFrame('This is private message to client with ID '.$id, 'STRING'); } // ( ) $appInstance->broadcastCall('sendBcMessage', array(\PHPDaemon\Core\Daemon::$process->getPid())); // 5 \PHPDaemon\Core\Timer::add(function($event) use ($appInstance) { $this->timerTask($appInstance); $event->finish(); }, 5e6); // } // ( ) public function sendBcMessage($pid) { foreach($this->sessions as $id=>$session) { $session->client->sendFrame('This is broadcast message from worker #'.$pid, 'STRING'); } } } class MyWebSocketRoute extends \PHPDaemon\WebSocket\Route { public $client; public $appInstance; public $id; // ID public function __construct($client,$appInstance) { $this->client=$client; $this->appInstance=$appInstance; } // public function onFrame($data, $type) { // $this->client->sendFrame('Server receive from client #'.$this->id.' message "'.$data.'"', 'STRING'); } // public function onFinish() { // unset($this->appInstance->sessions[$this->id]); } }
user www; group www; max-workers 8; min-workers 1; start-workers 1; max-idle 0; Pool:Servers\WebSocket { enable 1; listen "tcp://0.0.0.0"; listen-port 8047; privileged; } MyWebSocket {}
<script type="text/javascript"> function add(text) { document.forms[0].b.value=text+"\n"+document.forms[0].b.value; } if("WebSocket" in window) { var timer; var ws=new WebSocket("ws://site.com:8047/myws"); ws.onopen=function() { add('Connection opened'); timer=window.setInterval(function() { var date = new Date(); var message='ping at '+date.getSeconds(); ws.send(message); add('Client sent message "'+message+'"'); }, 30000); }; ws.onmessage=function(evt) { add('Message from server: "'+evt.data+'"'); }; ws.onclose=function() { add('Connection closed'); window.clearTimeout(timer); }; } else { alert("Your browser doesn't support WebSocket"); } </script> <form> <textarea name="b" style="width:100%;height:100%"/></textarea> </form>
# phpd restart
Source: https://habr.com/ru/post/168059/
All Articles