Details
-
Bug
-
Status: Open
-
Major
-
Resolution: Unresolved
-
0.11.0
-
None
Description
TEvhttpServer don't work with javascript client.
With c++ client or php client or other client, will work.
Example:
test1.thrift
namespace cpp test1 service test1 { i32 add_val( 1 : i32 a, 2 : i32 b ); }
thrift -r --gen cpp:cob_style test1.thrift thrift -r --gen php test1.thrift thrift -r --gen js test1.thrift
evhtp_thrift_server.cpp:
#include "../gen-cpp/test1.h" #include <thrift/protocol/TBinaryProtocol.h> #include <thrift/protocol/TJSONProtocol.h> #include <thrift/async/TAsyncProtocolProcessor.h> #include <thrift/async/TEvhttpServer.h> #include <thrift/server/TThreadedServer.h> #include <thrift/transport/THttpServer.h> #include <thrift/transport/TServerSocket.h> using namespace ::apache::thrift; using namespace ::apache::thrift::protocol; using namespace ::apache::thrift::transport; using namespace ::apache::thrift::server; using namespace ::apache::thrift::async; using boost::shared_ptr; using namespace ::test1; class test1Handler : virtual public test1If { public: int32_t add_val(const int32_t a, const int32_t b) { return a + b; } }; class test1AsyncHandler : public test1CobSvIf { public: test1AsyncHandler() { syncHandler_ = std::auto_ptr<test1Handler>( new test1Handler ); } void add_val(stdcxx::function <void(int32_t const& _ret)>cob, const int32_t a, const int32_t b ) { int32_t _ret = 0; _ret = syncHandler_->add_val(a, b); return cob(_ret); } protected: std::auto_ptr<test1Handler> syncHandler_; }; int main(int argc, char ** argv) { shared_ptr<test1AsyncHandler> handler(new test1AsyncHandler()); shared_ptr<TAsyncProcessor> proc(new test1AsyncProcessor(handler)); shared_ptr<TProtocolFactory> pfact(new TJSONProtocolFactory()); shared_ptr<TAsyncBufferProcessor> bufproc(new TAsyncProtocolProcessor(proc, pfact)); shared_ptr<TEvhttpServer> server(new TEvhttpServer(bufproc, 9090)); /* IF USE TThreadedServer WILL ALL WORK shared_ptr<test1Handler> test1( new test1Handler() ); shared_ptr<TProcessor> processor( new test1Processor(test1) ); shared_ptr<TTransportFactory> transportFactory( new THttpServerTransportFactory() ); shared_ptr<TProtocolFactory> protocolFactory( new TJSONProtocolFactory() ); shared_ptr<TServerTransport> serverTransport( new TServerSocket( 9090 ) ); shared_ptr<TThreadedServer> server( new TThreadedServer( processor, serverTransport, transportFactory, protocolFactory ) ); */ server->serve(); return 0; }
test1_client.cpp: (WORK)
#include <../gen-cpp/test1.h> #include <iostream> #include <thrift/transport/THttpClient.h> #include <thrift/transport/TSocket.h> #include <thrift/transport/TBufferTransports.h> #include <thrift/protocol/TJSONProtocol.h> using namespace apache::thrift; using namespace apache::thrift::protocol; using namespace apache::thrift::transport; int main(int argc, char ** argv) { boost::shared_ptr<TTransport> transport(new THttpClient("127.0.0.1", 9090, "/")); boost::shared_ptr<TProtocol> protocol(new TJSONProtocol(transport)); test1::test1Client client(protocol); transport->open(); std::cout << client.add_val( 5, 10 ) << std::endl; transport->close(); return 0; }
test1_client.php (WORK)
<? error_reporting( E_ALL ); $GLOBALS['THRIFT_ROOT'] = '/usr/lib/php'; require_once $GLOBALS['THRIFT_ROOT'].'/Thrift/ClassLoader/ThriftClassLoader.php'; use Thrift\ClassLoader\ThriftClassLoader; $loader = new ThriftClassLoader(); $loader->registerNamespace( 'Thrift', $GLOBALS['THRIFT_ROOT'] ); $loader->register(); use Thrift\Protocol\TJSONProtocol; use Thrift\Transport\TSocket; use Thrift\Transport\THttpClient; use Thrift\Transport\TBufferedTransport; use Thrift\Transport\TFramedTransport; use Thrift\Exception\TException; $GEN_DIR = 'gen-php'; require_once $GEN_DIR.'/test1.php'; require_once $GEN_DIR.'/Types.php'; try { $socket = new THttpClient( '127.0.0.1', 9090 ); $transport = new TBufferedTransport( $socket, 1024, 1024 ); $protocol = new TJSONProtocol( $transport ); $client = new test1Client( $protocol ); $transport->open(); echo $client->add_val( 5, 10 ); $transport->close(); } catch ( TException $tx ) { print 'TException: '.$tx->getMessage()."\n"; } ?>
test1_client.html (DON'T WORK)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Test Thrift</title> </head> <body> <input type="button" id="get_msg" value="Get Message" > <div id="output"></div> <script src="thrift.js"></script> <script src="gen-js/test1.js"></script> <script src="gen-js/test1_types.js"></script> <script> (function() { var transport = new Thrift.TXHRTransport( "http://127.0.0.1:9090" ); var protocol = new Thrift.TJSONProtocol( transport ); var client = new test1Client( protocol ); var outputElement = document.getElementById("output"); document.getElementById("get_msg").addEventListener("click", function(){ outputElement.innerHTML = client.add_val( 5, 10 ); }); })(); </script> </body> </html>
Help please.