Search code examples
javanode.jsthrift

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


I am trying to use node.js visit TThreadedSelectorServer implemented by Java

I have already been able to call the function defined in thrift using TThreadPoolServer, but I came across some problems when using TThreadPoolSelectorServer

this is my node.js client code

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);
    });

this is my java server code

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();
        }

I was able to use java client to visit this server successfully

@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);

    }

But I came across the error when using node.js client to visit that sever.

the error log in the server side was

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

but I have already assign TFramedTransport in node.js code

 transport = ThriftTransports.TFramedTransport();

Any help would be appreciated, Thanks in advance


Solution

  • Well , I just found the answer This problem is caused by TTFramedTransport i found even when I use this in my code

    transport = ThriftTransports.TFramedTransport();
    

    the transport seems not successfully been assigned TFramedTransport

    I just try this way

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

    it succeeded

    I am not familiar with node.js , maybe I figure out why this happen later