| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 1 | Thrift Javascript Library | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 2 | ========================= | 
 | 3 | This browser based Apache Thrift implementation supports | 
 | 4 | RPC using the JSON protocol over Http[s] with XHR. | 
| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 5 |  | 
 | 6 | License | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 7 | ------- | 
| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 8 | Licensed to the Apache Software Foundation (ASF) under one | 
 | 9 | or more contributor license agreements. See the NOTICE file | 
 | 10 | distributed with this work for additional information | 
 | 11 | regarding copyright ownership. The ASF licenses this file | 
 | 12 | to you under the Apache License, Version 2.0 (the | 
 | 13 | "License"); you may not use this file except in compliance | 
 | 14 | with the License. You may obtain a copy of the License at | 
 | 15 |  | 
 | 16 |   http://www.apache.org/licenses/LICENSE-2.0 | 
 | 17 |  | 
 | 18 | Unless required by applicable law or agreed to in writing, | 
 | 19 | software distributed under the License is distributed on an | 
 | 20 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
 | 21 | KIND, either express or implied. See the License for the | 
 | 22 | specific language governing permissions and limitations | 
 | 23 | under the License. | 
 | 24 |  | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 25 | Example | 
 | 26 | ------- | 
 | 27 | The listing below demonstrates a simple browser based JavaScript | 
 | 28 | Thrift client and Node.js JavaScript server for the HelloSvc service.  | 
| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 29 |  | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 30 | ### hello.thrift - Service IDL | 
 | 31 |     service HelloSvc { | 
 | 32 |         string hello_func(), | 
 | 33 |     } | 
| T Jake Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 34 |  | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 35 | ### 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 Luciani | 322caa2 | 2010-02-15 03:24:55 +0000 | [diff] [blame] | 59 |  | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 60 | ### 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); |