测试代码
1.server.php:
<?php
$serv = new swoole_server("0.0.0.0", 9502);
$serv->on('connect', function ($serv, $fd){
echo "Client:Connect. ";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, 'Swoole: '.$data);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close. ";
});
$serv->start();
?>
2.client.php:
<?php
$client = new swoole_client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli) {
$cli->send("hello Swoole ");
});
$client->on("receive", function($cli, $data){
echo "Receive: $data ";
});
$client->on("error", function($cli){
echo "connect fail ";
});
$client->on("close", function($cli){
echo "close ";
});
$client->connect('127.0.0.1', 9502, 0.5);
?>
Test:
先打开一个命令窗口,运行
# php server.php
同时再打开一个命令窗口,运行
# php client.php
成功运行