[英]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 我正在尝试使用node.js访问由Java实现的TThreadedSelectorServer
I have already been able to call the function defined in thrift using TThreadPoolServer, but I came across some problems when using TThreadPoolSelectorServer 我已经能够使用TThreadPoolServer调用Thrift中定义的函数,但是在使用TThreadPoolSelectorServer时遇到了一些问题
this is my node.js client code 这是我的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);
});
this is my java server code 这是我的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();
}
I was able to use java client to visit this server successfully 我能够使用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);
}
But I came across the error when using node.js client to visit that sever. 但是在使用node.js客户端访问该服务器时遇到了错误。
the error log in the server side was 服务器端的错误日志是
ERROR server.AbstractNonblockingServer$FrameBuffer (AbstractNonblockingServer.java:read(348)) - Read an invalid frame size of -2147418111.
错误server.AbstractNonblockingServer $ FrameBuffer(AbstractNonblockingServer.java:read(348))-读取无效的帧大小为-2147418111。 Are you using TFramedTransport on the client side?
您是否在客户端上使用TFramedTransport?
but I have already assign TFramedTransport in node.js code 但是我已经在node.js代码中分配了TFramedTransport
transport = ThriftTransports.TFramedTransport();
Any help would be appreciated, Thanks in advance 任何帮助,将不胜感激,在此先感谢
Well , I just found the answer This problem is caused by TTFramedTransport i found even when I use this in my code 好吧,我刚刚找到了答案这个问题是由即使我在代码中使用TTFramedTransport也导致的
transport = ThriftTransports.TFramedTransport();
the transport seems not successfully been assigned TFramedTransport 传输似乎未成功分配给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 我不熟悉node.js,也许我知道为什么以后会发生这种情况
The tutorial in http://thrift.apache.org/tutorial/nodejs is lying or maybe outdated, you must write like this to make it work: 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
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.