与第三方程序交互


使用CP的Delegate::get()可以很容易的与第三方程序进行交互

Yar

与YAR协作

require '../../crossboot.php';

$conf = array(
    'server'    =>  array(
        'id'    =>  2,
        'name'  =>  'test',
        'ver'   =>  '1.1',
    ),
);

$service = new Yar_Server( Cross\Delegate::loadApp( 'api', $conf ) );
$service->handle();

ZMQ

与ZMQ协作处理sokcet

require '../../crossboot.php';

$conf = array(
    'server'    =>  array(
        'id'    =>  2,
        'name'  =>  'test',
        'ver'   =>  '1.1',
    ),
);

$context = new ZMQContext(1);

//  Socket to talk to clients
$responder = new ZMQSocket($context, ZMQ::SOCKET_REP);
$responder->bind("tcp://127.0.0.1:5678");

while (true) {
    //  Wait for next request from client
    $request = $responder->recv();

    $request_array = array();
    parse_str( $request, $request_array );

    $controller = isset($request_array['mode']) ? $request_array['mode'] : '';
    if( $controller )
    {
        if(false !== strpos($controller, '.')) {
            $controller = str_replace('.', ':', $controller);
        } else {
            $controller = "{$controller}:index";
        }
        unset($request_array['mode']);
    } else {
        $controller = 'main:index';
    }

    try {
        $req = Cross\Delegate::loadApp( 'api', $conf )->get( $controller, $request_array, true );
    } catch(Exception $e) {
        $req = $e->getMessage();
    }

    $responder->send( $req );
}