<?php define('DEBUG', 'on'); define("WEBPATH", str_replace("\","/", __DIR__)); require __DIR__ . '/../libs/lib_config.php'; class WebSocket extends SwooleNetworkProtocolWebSocket { /** * 下线时,通知所有人 */ 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 SwooleLogEchoLog(true)); //Logger $server = new SwooleNetworkServer('0.0.0.0', 9503); $server->setProtocol($AppSvr); //$server->daemonize(); //作为守护进程 $server->run(array('worker_num' =>4));
更多使用swoole扩展php websocket示例相关文章请关注php中文网!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号