blob: 98a1b54761ba5dc0ad2ec65c0dcc5e5612d4c331 [file] [log] [blame]
T Jake Luciani322caa22010-02-15 03:24:55 +00001Thrift Javascript Library
Henrique Mendonça095ddb72013-09-20 19:38:03 +02002=========================
3This browser based Apache Thrift implementation supports
4RPC using the JSON protocol over Http[s] with XHR.
T Jake Luciani322caa22010-02-15 03:24:55 +00005
6License
Henrique Mendonça095ddb72013-09-20 19:38:03 +02007-------
T Jake Luciani322caa22010-02-15 03:24:55 +00008Licensed to the Apache Software Foundation (ASF) under one
9or more contributor license agreements. See the NOTICE file
10distributed with this work for additional information
11regarding copyright ownership. The ASF licenses this file
12to you under the Apache License, Version 2.0 (the
13"License"); you may not use this file except in compliance
14with the License. You may obtain a copy of the License at
15
16 http://www.apache.org/licenses/LICENSE-2.0
17
18Unless required by applicable law or agreed to in writing,
19software distributed under the License is distributed on an
20"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21KIND, either express or implied. See the License for the
22specific language governing permissions and limitations
23under the License.
24
Henrique Mendonça095ddb72013-09-20 19:38:03 +020025Example
26-------
27The listing below demonstrates a simple browser based JavaScript
28Thrift client and Node.js JavaScript server for the HelloSvc service.
T Jake Luciani322caa22010-02-15 03:24:55 +000029
Henrique Mendonça095ddb72013-09-20 19:38:03 +020030### hello.thrift - Service IDL
31 service HelloSvc {
32 string hello_func(),
33 }
T Jake Luciani322caa22010-02-15 03:24:55 +000034
Henrique Mendonça095ddb72013-09-20 19:38:03 +020035### hello.html - Browser Client
36 <!doctype html>
37 <html lang="en">
38 <head>
39 <script src="thrift.js" type="text/javascript"></script>
40 <script src="gen-js/HelloSvc.js" type="text/javascript"></script>
41 </head>
42 <body>
43 <h1>Apache Thrift JavaScript Browser Client Demo</h1>
44 <button id="btn">Get Message from Node Server</button>
45 <script type="text/javascript">
46 document.getElementById("btn").addEventListener("click", getMessage, false);
47
48 function getMessage() {
49 var transport = new Thrift.Transport("http://localhost:8585");
50 var protocol = new Thrift.Protocol(transport);
51 var client = new HelloSvcClient(protocol);
52 var msg = client.hello_func();
53 document.getElementById("output").innerHTML = msg;
54 }
55 </script>
56 <h2>Server Response: <div id="output"></div></h2>
57 </body>
58 </html>
T Jake Luciani322caa22010-02-15 03:24:55 +000059
Henrique Mendonça095ddb72013-09-20 19:38:03 +020060### hello.js - Node Server
61 var thrift = require('thrift');
62 var TJSONProtocol = require('thrift/protocol').TJSONProtocol;
63 var HelloSvc = require('./gen-nodejs/HelloSvc.js');
64
65 var call_counter = 0;
66
67 var server = thrift.createHttpGetPostServer(HelloSvc, {
68 hello_func: function(result) {
69 console.log("Client call: " + (++call_counter));
70 result(null, "Hello Apache Thrift for JavaScript " + call_counter);
71 }
72 }, {protocol: TJSONProtocol});
73
74 server.listen(8585);