Apache Thrift Javascript client to C++ Server

360 views Asked by At

I am having problems writing a Javascript client that interacts with a C++ server. I know Javascript only accepts JSON so I thought that if all I had to do was change the server protocol to JSON I thought it might work but its not. I am getting a weird error. I am using Firefox to run the Javascript client with http://localhost:9090/hello.html

error:

    Thrift: Mon Jun 15 16:54:07 2015 TSimpleServer exception: N6apache6thrift8protocol18TProtocolExceptionE: Expected '['; got 'G'.

Javascript Client

    <!DOCTYPE html>
    <!-- Simple Apache Thrift Client -->
    <html lang="en">
      <head>
        <meta charset="utf-8">
        <title>Hello Thrift</title>
      </head>
      <body>
        Name: 
        <input type="text" id="name_in">
        <input type="button" id="get_msg" value="Get Message" >
        <div id="output"></div>
        <script src="bower_components/thrift/lib/js/src/thrift.js"></script>
        <script src="gen-js/helloSvc.js"></script>
        <script>
          (function() {
            <!-- Setup Apache Thrift Client -->
            var transport = new Thrift.Transport("/nodejs/hello");
            var protocol  = new Thrift.Protocol(transport);
            var client    = new helloSvcClient(protocol);
            <!-- Wire Apache Thrift RPC to DOM Click Event -->
            var nameElement = document.getElementById("name_in");
            var outputElement = document.getElementById("output");
            document.getElementById("get_msg")
              .addEventListener("click", function(){
                outputElement.innerHTML = client.getMessage(nameElement.value);
              });
          })();
        </script>
      </body>
    </html>

C++ Server

    #include "helloSvc.h"
    #include <thrift/protocol/TBinaryProtocol.h>
    #include <thrift/protocol/TJSONProtocol.h>
    #include <thrift/server/TSimpleServer.h>
    #include <thrift/transport/TServerSocket.h>
    #include <thrift/transport/TBufferTransports.h>
    #include <thrift/server/TThreadedServer.h>
    #include <thrift/transport/TTransport.h>

    using namespace ::apache::thrift;
    using namespace ::apache::thrift::protocol;
    using namespace ::apache::thrift::transport;
    using namespace ::apache::thrift::server;

    using boost::shared_ptr;

    class helloSvcHandler : virtual public helloSvcIf {
     public:
      helloSvcHandler() {
        // Your initialization goes here
      }

      void getMessage(std::string& _return, const std::string& name) {
        // Your implementation goes here
        printf("getMessage\n");
            std::cout<<name<<std::endl;
            _return = "Hello from server";
      }

    };

    int main(int argc, char **argv) {
      int port = 9090;
      shared_ptr<helloSvcHandler> handler(new helloSvcHandler());
      shared_ptr<TProcessor> processor(new helloSvcProcessor(handler));
      shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
      shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
      shared_ptr<TProtocolFactory> protocolFactory(new TJSONProtocolFactory());
      TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
      std::cout<<"Starting server"<<std::endl;
      server.serve();
      return 0;
    }
0

There are 0 answers