时时商务社区
标题:
使用swoole扩展php websocket示例
[打印本页]
作者:
qz234
时间:
2018-2-14 06:00
[U]复制代码[/U] 代码如下:
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:
nClose($serv, $client_id, $from_id);
}
/**
* 接收到消息时
* @see WSProtocol:
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"); //加载配置文件
$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));
您可能感兴趣的文章:
php异步多线程swoole用法实例
php安装swoole扩展的方法
Swoole-1.7.22 版本已发布,修复PHP7相关问题
初识PHP中的Swoole
Swoole 1.10.0新版本发布,增加了多项新特性
欢迎光临 时时商务社区 (http://bbs.4435.cn/)
Powered by Discuz! X3.2