🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### **创建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 ```