Home >Backend Development >PHP Tutorial >Using swoole to extend php websocket example_PHP tutorial
class WebSocket extends SwooleNetworkProtocolWebSocket
{
/**
* Notify everyone when offline
*/
function onClose($serv, $client_id, $from_id)
{
//Close the message Send to everyone
//$this->log("onOffline: " . $client_id);
//$this->broadcast($client_id, "onOffline: " . $client_id);
parent::onClose($serv, $client_id, $from_id);
}
/**
* When a message is received
* @see WSProtocol::onMessage()
*/
function onMessage($client_id, $ws)
{
$this->log("onMessage: ".$client_id.' = '.$ ws['message']);
$this->send($client_id, "Server: ".$ws['message']);
//$this->broadcast($client_id, $ws['message']);
}
function broadcast($client_id, $msg)
{
foreach ($this->connections as $clid => $info)
{
if ($client_id != $ clid)
$AppSvr = new WebSocket();
$AppSvr->loadSetting(__DIR__."/swoole.ini"); //Load the configuration file
$AppSvr->setLogger(new SwooleLogEchoLog(true)); //Logger
$server->setProtocol($AppSvr);
//$server->daemonize(); //As a guardian Process
$server->run(array('worker_num' =>4));
http://www.bkjia.com/PHPjc/735249.html
true