多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### **创建TCP服务器** ***** <br> ### **示例:** ``` <?php class TcpServer{ public $serv = null; public function __construct(){ $this->serv = new swoole_server("0.0.0.0",9501); $this->serv->set([ 'worker_num'=> 4 ]); $this->serv->on('connect',[$this,'onConnect']); $this->serv->on('receive',[$this,'onReceive']); $this->serv->on('close',[$this,'onClose']); $this->serv->start(); } //监听连接进入事件 public function onConnect($serv,$fd){ echo "Client: Connect.\n"; } //监听数据接收事件 public function onReceive($serv, $fd, $from_id, $data) { $serv->send($fd, "Server: ".$data); } //监听连接关闭事件 public function onClose($serv, $fd) { echo "Client: Close.\n"; } } new TcpServer(); ``` ***** ### **测试:** ``` telnet 127.0.0.1 9501 hello Server: hello ```