Search code examples
javascriptthrift

javascript Thrift client hangs


I have the following Thrift client code in javascript:

<script language="javascript" type="text/javascript" src="thrift.js" />
<script language="javascript" type="text/javascript" src="QuantSvc_types.js" />
<script language="javascript" type="text/javascript" src="QuantSvc.js" />
<script language="javascript" type="text/javascript">
function calc() {   
    var transport = new Thrift.Transport("http://localhost:9997/QuantSvc/");   
    var protocol  = new Thrift.Protocol(transport);
    var client    = new QuantSvcClient(protocol);

    try {
        result = client.ListAllVariables()   
    } catch(ouch) {     
        alert("An exception occurred!")   
    } 
} 
</script>

Which is triggered when I push a button on my HTML page. Then, I have the following server-side Scala code, running on localhost:9997:

object Application extends App {
    val handler = new QuantSvcHandler()
    val processor = new QuantSvc.Processor(handler)
    val serverTransport = new TServerSocket(9997)
    val server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor))    
}

Where the QuantSvcHandler's ListAllVariables function is (basically a skeleton function, just trying to get things to work):

override def ListAllVariables(): util.List[Attributes] =
{
  var input = scala.collection.mutable.Buffer[Attributes]()

  input
}

I put a breakpoint at the first line of ListAllVariables, and also a few places in the QuantSvcHandler processor. I run the server in debug in intellij IDEA, open my HTML page in Chrome, and push the button (the one that calls the javascript calc() function). The button stays stuck and I see no kind of response on the server, the breakpoints aren't being hit.

Any ideas about what I'm doing wrong?


Solution

  • You mix a HTTP client with a socket server.

    Although HTTP uses sockets, the Thrift HTTP transport is not compatible with the Thrift Sockets transport. You need to set up the exact same protocol/transport stack on both ends. The only exception to that rule is that some server transports implicitly require an additional framed transport layer on the client side.

    So the solution is to use a HTTP server. Depending on the version you use, you may also have to switch to the JSON protocol.