php webscoket 扩展,使用swoole扩展php websocket示例

代码如下:

define('DEBUG', 'on');

define("WEBPATH", str_replace("\\","/", __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));

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

TAG标签:脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值