使用swoole扩展php websocket示例_PHP教程

原创
2016-07-13 10:37:31 813浏览

复制代码 代码如下:

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

class WebSocket extends Swoole\Network\Protocol\WebSocket
{
/**
* 下线时,通知所有人
*/
function onClose($serv, $client_id, $from_id)
{
//将下线消息发送给所有人
//$this->log("onOffline: " . $client_id);
//$this->broadcast($client_id, "onOffline: " . $client_id);
parent::onClose($serv, $client_id, $from_id);
}

/**
* 接收到消息时
* @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)
{
$this->send($clid, $msg);
}
}
}
}


$AppSvr = new WebSocket();
$AppSvr->loadSetting(__DIR__."/swoole.ini"); //加载配置文件
$AppSvr->setLogger(new \Swoole\Log\EchoLog(true)); //Logger

$server = new \Swoole\Network\Server('0.0.0.0', 9503);
$server->setProtocol($AppSvr);
//$server->daemonize(); //作为守护进程
$server->run(array('worker_num' =>4));

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/735249.htmlTechArticle复制代码 代码如下: ?php define('DEBUG', 'on'); define("WEBPATH", str_replace("\\","//m.sbmmt.com/m/", __DIR__)); require __DIR__ . '/../libs/lib_config.php'; class WebSocket extends Swool...

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:php去除字符串换行符示例分享_PHP教程 下一条:php的hash算法介绍_PHP教程

相关文章

查看更多