为什么即使指定了 TFramedTransport,Node JS 也会抱怨缺少 TFramedTransport?

Why does Node JS complain about a missing TFramedTransport even though TFramedTransport is specified?

我正在尝试使用 node.js 访问由 Java

实现的 TThreadedSelectorServer

我已经可以使用TThreadPoolServer调用thrift中定义的函数,但是在使用TThreadPoolSelectorServer时遇到了一些问题

这是我的 node.js 客户端代码

var thrift = require('thrift');
var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var CalculatorService = require('./gen-nodejs/CalculatorService.js');

//transport = ThriftTransports.TBufferedTransport();
transport = ThriftTransports.TFramedTransport();

protocol = ThriftProtocols.TBinaryProtocol();
//protocol = ThriftProtocols.TCompactProtocol();
//protocol = ThriftProtocols.TDebugProtocol();



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

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

// Create a Calculator client with the connection
var client = thrift.createClient(CalculatorService, connection);
var strList=new Array("Audi","BMW","Volvo");
var strMap={fistname:"bill",lastname:"gates",id:"1111"};

client.send_print(1,"hello,world",strList,strMap,function(err, response) {
      console.log("send_print result:" + response);
    });

这是我的java服务器代码

try
        {
            TNonblockingServerTransport serverTransport = new TNonblockingServerSocket(
                    9090);


            CalculatorService.Processor<CalculatorImpl> processor = new CalculatorService.Processor<CalculatorImpl>(
                    new CalculatorImpl());
            TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(
                    serverTransport).processor(processor);
            args.inputTransportFactory(new TFramedTransport.Factory())
            .outputTransportFactory(new TFramedTransport.Factory())
            .inputProtocolFactory(new TBinaryProtocol.Factory())
            .outputProtocolFactory(new TBinaryProtocol.Factory());

            TServer server = new TThreadedSelectorServer(args);
            System.out.println("Starting server on port 9090 ...");
            server.serve();
        } catch (TTransportException e)
        {
            e.printStackTrace();
        }

我能够使用 java 客户端成功访问此服务器

@Test
    public void test() throws TException
    {

        TSocket socket = new TSocket("192.168.129.186", 9090);

        TTransport transport = new TFramedTransport(socket);

        TProtocol protocol = new TBinaryProtocol(transport);

        CalculatorService.Client client = new CalculatorService.Client(protocol);

            transport.open();
            //socket.open();
            int num = 1;
            String str = "hello,world";
            ArrayList<String> strList = new ArrayList<String>();
            strList.add("hello,world1");
            strList.add("hello,world2");
            strList.add("hello,world3");

            HashMap<String, String> strMap = new HashMap<String, String>();
            strMap.put("hello1", "world1");
            strMap.put("hello2", "world2");
            strMap.put("hello3", "world3");
            int result = client.send_print(num, str, strList, strMap);
            //socket.close();
            transport.close();
            assertEquals(result, 3);

    }

但是我在使用node.js客户端访问该服务器时遇到了错误。

服务器端的错误日志是

ERROR server.AbstractNonblockingServer$FrameBuffer (AbstractNonblockingServer.java:read(348)) - Read an invalid frame size of -2147418111. Are you using TFramedTransport on the client side?

但我已经在 node.js 代码

中分配了 TFramedTransport
 transport = ThriftTransports.TFramedTransport();

任何帮助将不胜感激,提前致谢

嗯,我刚找到答案 这个问题是由 TTFramedTransport 引起的 我发现即使我在我的代码中使用它

transport = ThriftTransports.TFramedTransport();

传输似乎没有成功分配 TFramedTransport

我就这样试试

transport = require('./node_modules/thrift/framed_transport.js')

成功了

我不熟悉 node.js ,也许我稍后会弄清楚为什么会这样

http://thrift.apache.org/tutorial/nodejs中的教程是骗人的或者可能已经过时了,你必须这样写才能使它生效:

var thrift = require('thrift');
var ThriftTransports = require('thrift/transport');
var ThriftProtocols = require('thrift/protocol');
var CalculatorService = require('./gen-nodejs/CalculatorService.js');

var connection = thrift.createConnection("192.168.129.186", 9090, {
  transport : ThriftTransports.TFramedTransport,
  protocol : ThriftProtocols.TBinaryProtocol
});