源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

使用swoole扩展php websocket示例

  • 时间:2022-01-30 14:49 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:使用swoole扩展php websocket示例
[u]复制代码[/u] 代码如下:
<?php 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));
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部