Copy Code code as follows:
<?php
Define (' DEBUG ', ' on ');
Define ("Webpath", Str_replace ("\", "/", __dir__));
Require __dir__. '/.. /libs/lib_config.php ';
Class WebSocket extends Swoole\network\protocol\websocket
{
/**
* When offline, notify everyone
*/
function OnClose ($serv, $client _id, $from _id)
{
//Send offline message 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::o Nmessage ()
*/
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)
{
$this->send ($clid, $msg);
}
}
}
}
$APPSVR = new WebSocket ();
$APPSVR->loadsetting (__dir__.) /swoole.ini "); Load configuration file
$APPSVR->setlogger (New \swoole\log\echolog (true)); Logger
$server = new \swoole\network\server (' 0.0.0.0 ', 9503);
$server->setprotocol ($APPSVR);
$server->daemonize (); As a daemon process
$server->run (Array (' Worker_num ' =>4));