//文件server.php
$server = new swoole_server("127.0.0.1",8088,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);
$server->on('connect', function ($serv, $fd){
echo "Client:Connect.\n"; //启动一个循环,定时向客户端发一个消息 });
$server->on('receive', function ($serv, $fd, $from_id, $data) {
//我们修改一下on reveive的回调,然后启动服务
$server->on('receive', function ($serv, $fd, $from_id, $data) {
//根据收到的消息做出不同的响应
switch($data) {
case 1: { $serv->send($fd,"1 for apple\n"); break; }
case 2: { $serv->send($fd,"2 for boy\n"); break; }
default: { $serv->send($fd,"Others is default\n"); }
}
});
});
$server->on('close', function ($serv, $fd) { echo "Client: Close.\n"; });
$server -> start();
# telnet 127.0.0.1 8088
没反应,是因为这是服务端的,你得再开一个终端命令行并且cd到项目文件目录,然后
php server.php
#telnet 127.0.0.1 8088
这是输入1 就有会响应
排版怎么排啊,真麻烦
网友评论