官方例子怎么不行呢

<?php
class Server
{

private $serv;

public function __construct() {
    $this->serv = new swoole_server("0.0.0.0", 9501);
    $this->serv->set(array(
        'worker_num' => 8,
        'daemonize' => false,
    ));

    $this->serv->on('Start', 'onStart');
    $this->serv->on('Connect',  'onConnect');
    $this->serv->on('Receive',  'onReceive');
    $this->serv->on('Close', 'onClose');

    $this->serv->start();
}

public function onStart( $serv ) {
    echo "Start";
}

public function onConnect( $serv, $fd, $from_id ) {
    $serv->send( $fd, "Hello {$fd}!" );
}

public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
    echo "Get Message From Client {$fd}:{$data}";
    $serv->send($fd, $data);
}

public function onClose( $serv, $fd, $from_id ) {
    echo "Client {$fd} close connection";
}

}
// 启动服务器 Start the server
$server = new Server();

[root@iZwz96o0ziwb6z9x30b2oiZ jiahuatest]# php server.php
PHP Fatal error: Uncaught TypeError: Argument 2 passed to SwooleServer::on() must be callable, string given in /jiahuatest/server.php:13
Stack trace:

0 /jiahuatest/server.php(13): SwooleServer->on('Start', 'onStart')

1 /jiahuatest/server.php(39): Server->__construct()

2 {main}

thrown in /jiahuatest/server.php on line 13

Fatal error: Uncaught TypeError: Argument 2 passed to SwooleServer::on() must be callable, string given in /jiahuatest/server.php:13
Stack trace:

0 /jiahuatest/server.php(13): SwooleServer->on('Start', 'onStart')

1 /jiahuatest/server.php(39): Server->__construct()

2 {main}

thrown in /jiahuatest/server.php on line 13

阅读 2.7k
1 个回答

Server启动了你要启动client去连接啊- -。
这个Server启动以后会输出Start,你输出了吗?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进