douzangdang2225 2016-04-14 13:50
浏览 79

thrift nodejs客户端和php服务器一起工作?

I plan to use nodejs as the client, and php as the server. I use the official tutorial.thrift.

I set up a nginx server to host the localhost:9099 with the dir /path/to/my/tutorial/, generate a server script as index.php and place it in the root ,when visit the localhost:9099,so that client visit the server through index.php. the index.php content:

<?php

error_reporting(E_ALL);

require_once __DIR__ . "/Thrift/ClassLoader/ThriftClassLoader.php";
use Thrift\ClassLoader\ThriftClassLoader;

$ROOT = realpath(dirname(__FILE__));
$GEN_DIR = $ROOT.'/gen-php';

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $ROOT);
$loader->registerNamespace('Swoole', $ROOT);
$loader->registerNamespace('tutorial', $GEN_DIR);
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();

if (php_sapi_name() == 'cli') {
    ini_set("display_errors", "stderr");
}

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TPhpStream;
use Thrift\Transport\TBufferedTransport;
use Thrift\Server\TServer;

header('Content-Type', 'application/x-thrift');
if (php_sapi_name() == 'cli') {
    echo "
";
}

$handler = new \tutorial\Handler();
$processor = new \tutorial\CalculatorProcessor($handler);

$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TBinaryProtocol($transport, true, true);

$transport->open();
$processor->process($protocol, $protocol);
$transport->close();

the client.node.js content:

/**
 * Created by Kron on 16/4/12.
 */
var thrift = require('thrift');
var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var Calculator = require('../gen-nodejs/Calculator');
var ttypes = require('../gen-nodejs/tutorial_types');

transport = ThriftTransports.TBufferedTransport();
protocol = ThriftProtocols.TBinaryProtocol();


var connection = thrift.createConnection("localhost", 9099, {
    transport : transport,
    protocol : protocol
});

var client = thrift.createClient(Calculator, connection);


connection.on('error', function(err) {
    console.log(err);
    //assert(false, err);
});

// Create a Calculator client with the connection

client.ping(function(err, response) {
    console.log('ping()');
});

client.add(1,1, function(err, response) {
    console.log("1+1=" + response);
});

work = new ttypes.Work();
work.op = ttypes.Operation.DIVIDE;
work.num1 = 1;
work.num2 = 0;

client.calculate(1, work, function(err, message) {
    if (err) {
        console.log("InvalidOperation " + err);
    } else {
        console.log('Whoa? You know how to divide by zero?');
    }
});

work.op = ttypes.Operation.SUBTRACT;
work.num1 = 15;
work.num2 = 10;

client.calculate(1, work, function(err, message) {
    console.log('15-10=' + message);

    client.getStruct(1, function(err, message){
        console.log('Check log: ' + message.value);

        //close the connection once we're done
        connection.end();
    });
});

The client.php content:

<?php

error_reporting(E_ALL);

require_once __DIR__ . "/../Thrift/ClassLoader/ThriftClassLoader.php";
use Thrift\ClassLoader\ThriftClassLoader;

$ROOT = realpath(dirname(__FILE__).'/../');
$GEN_DIR = $ROOT.'/gen-php';

$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', $ROOT);
$loader->registerNamespace('Swoole', $ROOT);
$loader->registerNamespace('tutorial', $GEN_DIR);
$loader->registerDefinition('shared', $GEN_DIR);
$loader->registerDefinition('tutorial', $GEN_DIR);
$loader->register();

use Thrift\Protocol\TBinaryProtocol;
use Thrift\Transport\TSocket;
use Thrift\Transport\THttpClient;
use Thrift\Transport\TBufferedTransport;
use Thrift\Exception\TException;

try {
    if (array_search('--http', $argv)) {
        $socket = new THttpClient('localhost', 9099, '/clients_servers/tutorial.server.php');
    } else {
        $socket = new THttpClient('localhost', 9099); // 8080 for node server, 9099 for php server
    }

    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);
    $client = new \tutorial\CalculatorClient($protocol);

    $transport->open();

    $client->ping();
    print "ping()
";

    $sum = $client->add(1,1);
    print "1+1=$sum
";

    $work = new \tutorial\Work();

    $work->op = \tutorial\Operation::DIVIDE;
    $work->num1 = 1;
    $work->num2 = 0;

    try {
        $client->calculate(1, $work);
        print "Whoa! We can divide by zero?
";
    } catch (\tutorial\InvalidOperation $io) {
        print "InvalidOperation: $io->why
";
    }

    $work->op = \tutorial\Operation::SUBTRACT;
    $work->num1 = 15;
    $work->num2 = 10;
    $diff = $client->calculate(1, $work);
    print "15-10=$diff
";

    $log = $client->getStruct(1);
    print "Log: $log->value
";

    $transport->close();

} catch (TException $tx) {
    print 'TException: '.$tx->getMessage()."
";
}

They're almost the same as original.

  1. when I use client.php visit the host, It works well.
  2. when I use client.node.js visit the host, It doesn't work.
  3. when I try to use a node server,not the host, visit it use the client.php,I have to change $socket = new THttpClient('localhost', 9099) to $socket = new TSocket('localhost', 9099) to make it work.
  4. client.node.js never ever touched the localhost:9099, and without any stdout.
  • 写回答

2条回答 默认 最新

  • dougu4704 2016-04-15 08:30
    关注

    Finally my partner helped me found the answer.

    the official demo of nodejs cient has some problems:

    official code:

    transport = ThriftTransports.TFramedTransport();
    protocol = ThriftProtocols.TBinaryProtocol();
    

    our code:

    transport = ThriftTransports.TFramedTransport;
    protocol = ThriftProtocols.TBinaryProtocol;
    

    remove the (), that makes the code work.

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题