thrift nodejs客户端和php服务器一起工作?
我打算使用nodejs作为客户端,并将php作为服务器。我使用官方tutorial.thrift。thrift nodejs客户端和php服务器一起工作?
我成立了一个nginx的服务器来托管localhost:9099
与DIR /path/to/my/tutorial/
,生成一个服务器脚本index.php文件,并将其放置在根,请访问localhost:9099
时,让客户通过index.php
访问服务器。 的index.php
内容:
<?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 "\r\n";
}
$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();
的client.node.js
内容:
/**
* 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();
});
});
的client.php
内容:
<?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()\n";
$sum = $client->add(1,1);
print "1+1=$sum\n";
$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?\n";
} catch (\tutorial\InvalidOperation $io) {
print "InvalidOperation: $io->why\n";
}
$work->op = \tutorial\Operation::SUBTRACT;
$work->num1 = 15;
$work->num2 = 10;
$diff = $client->calculate(1, $work);
print "15-10=$diff\n";
$log = $client->getStruct(1);
print "Log: $log->value\n";
$transport->close();
} catch (TException $tx) {
print 'TException: '.$tx->getMessage()."\n";
}
他们几乎相同的原件。
- 当我用
client.php
访问主机,它工作正常。 - 当我使用
client.node.js
访问主机时,它不起作用。 - ,当我尝试使用节点服务器,不是主机,访问它使用
client.php
,我不得不改变$socket = new THttpClient('localhost', 9099)
到$socket = new TSocket('localhost', 9099)
,使其工作。 -
client.node.js
从未碰过localhost:9099
,没有任何标准输出。
最后我的搭档帮我找到了答案。
的NodeJS cient的官方演示有一些问题:
的官方代码:
transport = ThriftTransports.TFramedTransport();
protocol = ThriftProtocols.TBinaryProtocol();
我们的代码:
transport = ThriftTransports.TFramedTransport;
protocol = ThriftProtocols.TBinaryProtocol;
删除()
,使代码工作。
太好了 - 请考虑提供[补丁或拉请求!](https://thrift.apache.org/docs/HowToContribute) – JensG
[HBase的-DIR] /bin/hbase-daemon.sh开始-f
我面临同样的错误节俭。请参阅http://dailyjs.com/post/hbase 默认情况下,TFramedTransport处于禁用状态。您必须使用上述命令启用它。 注意:启用后无法连接php。我使用不同的协议,在PHP
感谢@JensG去除没用的话〜 –
这是一个总的政策上SO(SE和一般)。个人帮助哭泣和问候与预期的问答格式不符。所以这与你个人无关。 – JensG