Home >Backend Development >PHP Tutorial >Using swoole to extend php websocket example_PHP tutorial

Using swoole to extend php websocket example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:311305browse

Copy code The code is as follows:

define('DEBUG', 'on');
define("WEBPATH", str_replace("\","/", __DIR__));
require __DIR__ . '/../libs/lib_config.php';

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 = new SwooleNetworkServer('0.0.0.0', 9503);

$server->setProtocol($AppSvr);
//$server->daemonize(); //As a guardian Process
$server->run(array('worker_num' =>4));





http://www.bkjia.com/PHPjc/735249.html

www.bkjia.com

true

TechArticleCopy the code as follows: ?php define('DEBUG', 'on'); define("WEBPATH", str_replace("\","/", __DIR__)); require __DIR__ . '/../libs/lib_config.php'; class WebSocket extends Swool...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn