java - 为什么即使指定了 TFramedTransport,Node JS 也会提示缺少 TFramedTransport?

标签 java node.js thrift

我正在尝试使用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不熟悉,也许我稍后会明白为什么会发生这种情况

关于java - 为什么即使指定了 TFramedTransport,Node JS 也会提示缺少 TFramedTransport?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29689373/

相关文章:

Eclipse 中的 Java Swing JFontChooser "main not found"

java - 如何获得 Joda 日期之间的正确小时数?

javascript - 在 Node 和 Express 制作的 REST API 上管理分页和搜索/过滤器的正确方法

javascript - Node/ express 4 : displaying errors with express-validator on ajax post

c++ - Thrift 服务器 : detect client disconnections (C++ library)

c++ - Thrift 异步 C++ 示例

java - 任何保存 bundle 数据的方法

java - 如何将类名用作数据类型?

node.js - 如何找到 Node 的内置模块目录?

thrift - 如何使用带有多个生成器选项的 Thrift Java 代码生成器