如何将我自己的类添加到ratchat服务器

时间:2015-10-22 08:09:00

标签: php class ratchet

我使用棘轮创建websocket服务器,但我有一个问题....
如何编写我自己的类并将其用于此....

PHP:

    <?php
    namespace MyApp;
    use Ratchet\MessageComponentInterface;
    use Ratchet\ConnectionInterface;

    define ("ABSPATH" , "D:/MyProject/server/bin/");
    require_once ABSPATH."config.php";

    require_once ABSPATH."classes/clients.php";// <--- i required this file that
//it contain clients class 
//but when i want to use it i get error that this class notfound

    class Poker implements MessageComponentInterface {
        protected $clients;

        public function __construct(){   
            $this->clients = array();
        }
        public function onOpen(ConnectionInterface $conn) {

            $client = new clients($conn);//here i get error
            array_push($this->clients,$client);

        }
        public function onMessage(ConnectionInterface $from, $msg) {

    }
        public function onClose(ConnectionInterface $conn) {

        }
        public function onError(ConnectionInterface $conn, \Exception $e) {

        }
    }

我需要课程文件

require_once ABSPATH."classes\clients.php";

但在这一行我得到错误

$client = new clients($conn);// ERROR CLASS NOT FOUND

错误:

Fatal error: Class 'MyApp\clients' not found in D:\MyProject\server\src\MyAp
p\Poker.php on line 18

1 个答案:

答案 0 :(得分:3)

这是因为当前文件位于命名空间中,但我假设您的文件与客户端类不同?

尝试添加

    namespace MyApp;

到clients.php,如果你想创建一个新的专用命名空间,那么你必须use它就像你当前文件中的两行一样