直接用docker跑环境
docker pull xlight/docker-php7-swoole docker run -it -v ~/Projects/php/swoole:/workdir -p 9501:9501 xlight/docker-php7-swoole /bin/bash php workdir/app_server.php start
app_server.php
<?php $serv = new swoole_server("0.0.0.0", 9501); $serv->set(array( 'package_max_length' => 8192, 'open_length_check'=> true, 'package_length_offset' => 0, //大小端默认,组包方式{packet, 2} // 'package_body_offset' => 2, // 'package_length_type' => 'n' //大小端默认,组包方式{packet, 4} 'package_body_offset' => 4, 'package_length_type' => 'N' )); $port = $serv->listen('127.0.0.1', 9502, SWOOLE_SOCK_UDP); $port->on('packet', function($serv, $data, $addr){ var_dump($serv, $data, $addr); }); $serv->on('connect', function ($serv, $fd, $from_id){ echo "[#".posix_getpid()."] Client@[$fd:$from_id]: Connect. "; }); $serv->on('receive', function (swoole_server $serv, $fd, $from_id, $data) { echo "[#".$serv->worker_id."] Client[$fd]: $data "; if ($serv->send($fd, "hello ") == false) { echo "error "; } }); $serv->on('close', function ($serv, $fd, $from_id) { echo "[#".posix_getpid()."] Client@[$fd:$from_id]: Close. "; }); $serv->start();
参数参考地址