worker = new Worker(); $this->run(); } /** * 业务方法,业务写在这里 */ private function run(){ $this->worker->onWorkerStart = function ($worker) { $host = "ws://127.0.0.1:23456";//定义协议和端口,后续可以放到配置中 $con = new AsyncTcpConnection($host); $con->onConnect = function($con) { $tlv=new Tlv(); $tag = 0x01; $value = 'Hello i am client'; $length = strlen( $value ); $data = array( array($tag, $length,$value), array($tag, $length,$value) ); //组包 $data = $tlv->Write($data); $con->send($data); }; $con->onMessage = function($con, $data) { echo $data; //$con->send('hello,i am client'); }; $con->connect(); }; Worker::runAll(); } }